Skip to main content

字典 Dict

Declare

choice = {
"name": "beau",
"age": "30",
}
p_choice = choices["name"]
age_string = "age"
p_choice_2 = choices[age_string]
dog = {
"name": "Roger",
"age": 8,
"color": "green"
}
dog.get("name")
dog.get("color", "brown") # brown is default value
dog.pop("name") # Remove "name" key-value in dog dict
dog.popitem() # Remove last key-value in dog dict

"color" in dog

dog.keys()
list(dog.keys())
dog.values()
list(dog.values())
dog.items()
list(dog.items())

len(dog)
dog["favorite food"] = "Meat"
del dog["color"]

dogCopy = dog.copy()