Lists that store dictionaries, dictionaries that store lists, dictionaries that store dictionaries

///

alien_0={'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points']
Copy the code

Key-value pairs use colons instead of equal signs

///

Add key-value pairs

ailen_0['x_position']=0
alien_0['y_position']=25
Copy the code

///

Create an empty dictionary to add key-value pairs to an empty dictionary

alien_0={}
Copy the code

///

Modify values in the dictionary

alien_0['color']='yellow'
Copy the code

If the ‘color’ key exists, it is modified

If no, add

///

Delete key-value pairs (DEL statement)

del alien_0[‘points’]

A good dictionary format:

Favorite_languages = {' jen ':' python ', 'Sarah' : 'c', 'Edward' : 'ruby,' phil ':' python,}Copy the code

///

Iterate over all key-value pairs:

for key,value in user_0.items():
    print("\nkey:"+key)
    print("Value:"+value)
Copy the code

.

for name,language in favorite_languages.items(): Print (name.title()+"'s favorite language is "+language.title()+".") item() is a method that returns a list of key-value pairsCopy the code

///

Iterate over all keys (method keys ())

for name in favorite_language.key()
    print(name.title())
    
Copy the code

The key () method isn’t just for traversal; In fact, it returns a list of all the keys in the dictionary

You can use key () to determine whether a key is included in the dictionary

if ‘erin’ not in favorite_languages.key(): …

///

Walk through all the keys in the dictionary in order

But the order in which dictionary elements are retrieved is unpredictable

One way to return elements in a particular order is to sort the returned keys in the for loop.

To do this, use the function sorted () to get a copy of the list of keys in a particular order:

for name in sorted(favorite.lanuage.key()):
    print("name.title()+"+",thanks")
    
Copy the code

///

Iterate over all the values in the dictionary

If you are primarily interested in the values contained in the dictionary, use the method values ().

It returns a list of values without any keys.

for language in favorite_language.values():
    print(language.title())
    
Copy the code

If the value has a large number of duplicates, the duplicate items can be removed using set.

Collections are similar to lists, but each element must be unique

For language in set (favorite_language.values()) : print(language.title())Copy the code

///

nested

Dictionary list (embed dictionary in list)

Range () returns a series of numbers whose sole purpose is to tell Python how many times we need to repeat the loop

///

Modify the first three robots to yellow using slicing to operate on the dictionary list

///

List dictionary (store lists in dictionaries)

///

///

6.4.3 Storing dictionaries in Dictionaries

///