The dictionary

  • The data format consisting of key-value pairs is a dictionary. For example: ID card
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
Copy the code

Common dictionary operations

  • Print the value
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
print(card["name"])
print(card["age"])
print(card["address"])
Copy the code

Results of the above code:

  • Add elements
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
Add element if key exists, add element if key does not exist
card["sex"] = "Male"#sex key does not exist, add
card["age"] = 34The #age key exists, modified
print(card)

"' card. Setdefault (" sex", "female") # key, not modify card. Setdefault (" edu ", "Dr") # key does not exist, add ' ' '
Copy the code

Results of the above code:

  • Remove elements
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
#pop() removes elements
card.pop("name")Select * from name where key = name
print(card)
Copy the code

Results of the above code:

  • Modify the element
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
# modify element
card["name"] = "Xiao Ming"
print(card)
Copy the code

Results of the above code:

  • Look for the element
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
print(card["name"])
print(card["age"])
print(card["address"])
#print(card["sex"]

print(card.get("name"))
print(card.get("age"))
print(card.get("address"))
print(card.get("sex"))Return None if no key exists
Copy the code

Results of the above code:

  • Print all keys
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
Python3 returns iterators that are converted to lists by list()
print(list(card.keys()))
Copy the code

Results of the above code:

  • Print all values
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
Python3 returns iterators that are converted to lists by list()
print(list(card.values()))
Copy the code

Results of the above code:

  • Prints all key-value pairs
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
Python3 returns iterators that are converted to lists by list()
print(list(card.items()))
Copy the code

Results of the above code:

  • empty
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}
# Empty the dictionary
card.clear()
print(card)
Copy the code

Results of the above code:

traverse

  • Traverse by the key
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}

Traversal by key
for i in card.keys():
    print(card[i])Value by key
Copy the code

Results of the above code:

  • Traverse by value
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}

Traversal by value
for i in card.values():
    print(i)Print the value directly
Copy the code
  • Traversal by key value pairs
# Dictionary definition
card = {"name":Trinket."age":33."address":"Chang 'an Avenue, Beijing"}

Traversal by key/value pairs
for i in card.items():
    print(i)# return a tuple of key-value pairs
    print(i[0])Take the first value of the tuple
    print(i[1])Take the second value of the tuple

For k,v in card.items(): print(k) print(v) print(v)
Copy the code

Results of the above code: