Python Dictionaries
Dictionaries in Python
Python dictionaries are powerful data structures used to store data values in key-value pairs. This lesson will explore the characteristics of dictionaries, how to create and manipulate them, and various examples demonstrating their functionality.
1. What is a Dictionary?
A dictionary is a collection that is ordered*, changeable, and does not allow duplicates. As of Python version 3.7, dictionaries maintain the order of items. In earlier versions, dictionaries were unordered.
Dictionaries are defined using curly brackets {}
and consist of key-value pairs.
Example: Create and Print a Dictionary
1 2 3 4 5 6 7 8
# Creating a dictionary car_dict = { "brand": "Tesla", "model": "Model S", "year": 2020 } print(car_dict) # Output: {'brand': 'Tesla', 'model': 'Model S', 'year': 2020}
2. Dictionary Items
Dictionary items are presented in key-value pairs, and you can access them using the key names.
Example: Accessing a Dictionary Value
1 2 3 4 5 6 7 8 9
# Creating a dictionary car_dict = { "brand": "Tesla", "model": "Model S", "year": 2020 } # Accessing the "model" value print(car_dict["model"]) # Output: Model S
3. Ordered or Unordered?
As of Python 3.7, dictionaries are ordered collections. This means that items have a defined order, which will not change. In versions prior to 3.7, dictionaries were unordered.
Example: Checking Order
1 2 3 4 5 6 7 8
# Creating a dictionary color_dict = { "first": "red", "second": "green", "third": "blue" } print(color_dict) # Output will show the order of items as defined
4. Changeable
Dictionaries are mutable, meaning you can change, add, or remove items after they have been created.
Example: Modifying a Dictionary
1 2 3 4 5 6 7 8 9
# Creating a dictionary person_dict = { "name": "Alice", "age": 30 } # Changing the age person_dict["age"] = 31 print(person_dict) # Output: {'name': 'Alice', 'age': 31}
5. Duplicates Not Allowed
Dictionaries cannot have two items with the same key. If you use the same key again, the value will be overwritten.
Example: Duplicate Key Handling
1 2 3 4 5 6 7 8
# Creating a dictionary with a duplicate key product_dict = { "name": "Laptop", "price": 1000, "price": 1200 # This will overwrite the previous price } print(product_dict) # Output: {'name': 'Laptop', 'price': 1200}
6. Dictionary Length
To determine how many items a dictionary contains, use the len()
function.
Example: Get the Number of Items in a Dictionary
1 2 3 4 5 6 7 8
# Creating a dictionary item_dict = { "item1": "Book", "item2": "Pen", "item3": "Notebook" } print(len(item_dict)) # Output: 3
7. Dictionary Items - Data Types
The values in a dictionary can be of any data type, including strings, integers, booleans, and lists.
Example: Dictionary with Various Data Types
1 2 3 4 5 6 7 8 9
# Creating a dictionary with mixed data types mixed_dict = { "name": "Bob", "is_student": True, "age": 25, "courses": ["Math", "Science"] } print(mixed_dict) # Output: {'name': 'Bob', 'is_student': True, 'age': 25, 'courses': ['Math', 'Science']}
8. What is the Data Type of a Dictionary?
From Python's perspective, dictionaries are defined as objects of the data type 'dict'.
Example: Print the Data Type of a Dictionary
1 2 3 4 5 6 7 8
# Creating a dictionary my_dict = { "brand": "Toyota", "model": "Camry", "year": 2022 } print(type(my_dict)) # Output: <class 'dict'>