dimanche 5 septembre 2021

how to count the number of items in a list which is nested in a dictionary?

hey there I'm learning python and need to count the items in nested lists like below, so that I can give an answer based on if the person has one or more favourite languages:

favourite_languages = {
    'Joe': ['French'],
    'Jim': ['Spanish', 'Portugese'],
    'Alan':['German', 'Swedish']
}
for name, lang in favourite_languages.items():
    if len(name) == 1:
        print(f"{name}'s favourite language is {lang}")
    elif len(name > 1):
        print(f"{name}'s favourite languages are {lang}")

Thank you guys very much!

Aucun commentaire:

Enregistrer un commentaire