Python Lesson 13: Dictionaries in Python

  1. Create a Dictionary
  2. Accessing items in a Dictionary
  3. Change a value in a Dictionary
  4. Loop through a Dictionary and access the keys in the Dictionary
  5. Loop through a Dictionary and access the values in the Dictionary
  6. Loop through a Dictionary and access the values and keys in the Dictionary
  7. Check if a value exists in a Dictionary
  8. Get the length of a Dictionary
  9. Remove an item from a Dictionary using a key
  10. Clear all items from a Dictionary

Features of a Dictionary

A dictionary is a collection of objects. It contains the following features:

  • It is unordered
  • It is changeable
  • It is indexed
  • Uses key value pairs

1) Create a Dictionary

Dictionaries are created with an empty pair of curly brackets.

#Create a Dictionary
car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black"
        }
print(car)
Notebook Output

2) Accessing items in a Dictionary

You can access items using the key or use the get() method or use the key inside of square brackets.

car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black"
        }
#Use Key to access item
print(car["Make"])
#Use get method
print(car.get("Make"))
Notebook Output

3) Change a value in a Dictionary

Use the key to change a value in the dictionary.

car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black"
        }
#Change a value in a dictionary
car["Color"] = "Red"
print(car)
Notebook Output

4) Loop through a Dictionary and access the keys in the Dictionary

Use a for loop to iterate through the keys of a dictionary.

car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black"
        }
#print keys
for x in car:
    print(x)
Notebook Output

5) Loop through a Dictionary and access the values in the Dictionary

To loop through the values of a Dictionary use a for loop and put the looping value inside of square brackets:

#print values  
car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black"
        }
for x in car:
    print(car[x])
Notebook Output

6) Loop through a Dictionary and access the values and keys in the Dictionary

To loop through the values of a Dictionary use a for loop with 2 looping values:

#print both keys and values
car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black"
        }
for key,value in car.items():
    print (key, value)
Notebook Output

7) Check if a value exists in a Dictionary

Use the “in” keyword to check if a value exists in a Dictionary.

car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black"
        }
print("Make" in car)
Notebook Output

8) Get the length of a Dictionary

Use the len() method to get the size/length of a Dictionary.

car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black"
       }
print(len(car))
Notebook Output

9) Remove an item from a Dictionary using a key

Use the pop method to remove an item from a Dictionary.

car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black",
        "Year":2019
       }
#removes item at specified key
car.pop("Year")
print(car)
Notebook output

10) Clear all items from a Dictionary.

Use the clear() method to clear all items from a dictionary

car =  {"Make":"Honda",
        "Model":"Civic",
        "Color":"Black",
        "Year":2019
       }
car.clear()
print(car)
Notebook output

Recommended Books