- Create a Dictionary
- Accessing items in a Dictionary
- Change a value in a Dictionary
- Loop through a Dictionary and access the keys in the Dictionary
- Loop through a Dictionary and access the values in the Dictionary
- Loop through a Dictionary and access the values and keys in the Dictionary
- Check if a value exists in a Dictionary
- Get the length of a Dictionary
- Remove an item from a Dictionary using a key
- 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)

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"))

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)

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)

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])

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)

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)

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))

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)

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)

- Python Lesson 1: Hello World in Python
- Python Lesson 2: Reserved Keywords
- Python Lesson 3: Creating Variables
- Python Lesson 4: Performing math calculations in Python using operators
- Python Lesson 5: Working with Strings
- Python Lesson 6: Useful String Methods
- Python Lesson 7: Getting User Keyboard input
- Python Lesson 8: Importing Modules into your Python code
- Python Lesson 9: Decision making in Python
- Python Lesson 10: Creating loops in Python
- Python Lesson 11: Lists in Python
- Python Lesson 12: Tuples in Python
- Python Lesson 13: Dictionaries in Python
- Python Lesson 14: Sets in Python
- Python Lesson 15: Dealing with dates and time
- Python Lesson 16: Creating and using functions
- Python Lesson 17: Creating and using classes in Python
- Python Lesson 18: List Comprehensions
- Python Lesson 19: Lambda Functions
- Python Lesson 20: Error Handling