Lessons
Python Basics
Python Variables
Operators in Python
Conditional Statements in Python
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Loops in Python
Python Arrays and Functions
Conclusion
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
python
1# Creating a dictionary
2car_dict = {
3 "brand": "Tesla",
4 "model": "Model S",
5 "year": 2020
6}
7
8print(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
python
1# Creating a dictionary
2car_dict = {
3 "brand": "Tesla",
4 "model": "Model S",
5 "year": 2020
6}
7
8# Accessing the "model" value
9print(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
python
1# Creating a dictionary
2color_dict = {
3 "first": "red",
4 "second": "green",
5 "third": "blue"
6}
7
8print(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
python
1# Creating a dictionary
2person_dict = {
3 "name": "Alice",
4 "age": 30
5}
6
7# Changing the age
8person_dict["age"] = 31
9print(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
python
1# Creating a dictionary with a duplicate key
2product_dict = {
3 "name": "Laptop",
4 "price": 1000,
5 "price": 1200 # This will overwrite the previous price
6}
7
8print(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
python
1# Creating a dictionary
2item_dict = {
3 "item1": "Book",
4 "item2": "Pen",
5 "item3": "Notebook"
6}
7
8print(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
python
1# Creating a dictionary with mixed data types
2mixed_dict = {
3 "name": "Bob",
4 "is_student": True,
5 "age": 25,
6 "courses": ["Math", "Science"]
7}
8
9print(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
python
1# Creating a dictionary
2my_dict = {
3 "brand": "Toyota",
4 "model": "Camry",
5 "year": 2022
6}
7
8print(type(my_dict)) # Output: <class 'dict'>