Python Nesting-Lists in a dictionary

Python Nesting-Lists in a dictionary





Nesting-Lists in a dictionary

Storing a list inside a dictionary alows you to associate more thanone value with each key.

#storing lists in a dictionary
#store multiple languages for each person.
fav_languages = {
 'jen': ['python', 'ruby'],
 'sarah': ['c'],
 'edward': ['ruby', 'go'],
 'phil': ['python', 'haskell'],
}
# Show all responses for each person.
for name, langs in fav_languages.items():
 print(name + ":")
 for lang in langs:
  print("-" + lang)

Screenshot 

nesting list

 

0 comments:

Post a Comment