dictionary = {
"key": "value",
}Example:
bio = {
"name": "naz",
"age": 31,
}
# {'name': 'naz', 'age': 31}bio["is_employed"] = True
# {'name': 'naz', 'age': 31, 'is_employed': True}bio = {
"name": "naz",
"age": 31,
}
bio["name"] = "Ash"
# {'name': 'Ash', 'age': 31}bio = {
"name": "naz",
"age": 31,
}
bio = {}
# {}
# {'name': 'Ash', 'age': 31}bio = {
"name": ["Ash", "Max", "Naz"],
"age": 31,
}
bio = {
"name": ["Ash", "Max", "Naz", ["Alex", "John"]],
"age": 31,
} bio = {
"name": ["Ash", "Max", "Naz"],
"age": 31,
}
print(bio["name"][1])
# Maxbio = {
"name": ["Ash", "Max", "Naz", ["Alex", "John"]],
"age": 31,
}
print(bio["name"][3][1])
# John user_profile = {
"name": "Alex",
"contact": {
"email": "alex@example.com",
"phone": "123-456-7890",
},
"status": "Active",
}
print(user_profile["contact"]["phone"])
# 123-456-7890When we loop through a dictionary, it loops through the `keys.
In this example each fruit is a key and the color is the value.
color_box = {
"apple": "red",
"orange": "orange",
"peach": "pink",
"banana": "yellow",
}
# looping throgh `keys`
for fruit in color_box:
print(fruit)
# looping through `values`
for fruit in color_box:
print(color_box[fruit])I wrote an article wite more details about looping through a dictionary.
You can check it out HERE