Skip to content

Latest commit

 

History

History
143 lines (99 loc) · 1.93 KB

File metadata and controls

143 lines (99 loc) · 1.93 KB

Dictionary

dictionary = {
    "key": "value",
}

Example:

bio = {
    "name": "naz",
    "age": 31,
}

# {'name': 'naz', 'age': 31}

Assigning a new key

bio["is_employed"] = True
# {'name': 'naz', 'age': 31, 'is_employed': True}

Reassigning a new key

bio = {
    "name": "naz",
    "age": 31,
}

bio["name"] = "Ash"

# {'name': 'Ash', 'age': 31}

Wiping/deleting the dictionary

bio = {
    "name": "naz",
    "age": 31,
}

bio = {}

# {}

# {'name': 'Ash', 'age': 31}

Nesting lists inside a dictionary

bio = {
    "name": ["Ash", "Max", "Naz"],
    "age": 31,
}

bio = {
    "name": ["Ash", "Max", "Naz", ["Alex", "John"]],
    "age": 31,
} 

How to print out the values inside the nested lists?

bio = {
    "name": ["Ash", "Max", "Naz"],
    "age": 31,
}

print(bio["name"][1])
# Max
bio = {
    "name": ["Ash", "Max", "Naz", ["Alex", "John"]],
    "age": 31,
}

print(bio["name"][3][1])
# John 

A nested dictionary

user_profile = {
    "name": "Alex",
    "contact": {
        "email": "alex@example.com",
        "phone": "123-456-7890",
    },
    "status": "Active",
}


print(user_profile["contact"]["phone"])
# 123-456-7890

How to loop through a dictionary

When 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