Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Appending a dictionary to a list in Python
In Python, dictionaries store key-value pairs while lists store multiple values in sequence. Appending dictionaries to lists is useful for collecting structured data, such as storing multiple records or configurations in a single container.
Basic List and Dictionary Overview
A list is an ordered collection of items enclosed in square brackets []. A dictionary is an ordered collection of key-value pairs enclosed in curly braces {}.
# List example
sample_list = ["Apple", "Banana", "Cherry"]
print("List:", sample_list)
# Dictionary example
student = {"name": "John", "age": 25, "grade": "A"}
print("Dictionary:", student)
List: ['Apple', 'Banana', 'Cherry']
Dictionary: {'name': 'John', 'age': 25, 'grade': 'A'}
Using append() Method
The append() method adds a dictionary as a single element to the end of a list ?
# Create a list with existing elements
students = ["Math", "Science"]
# Create a dictionary
student_info = {"name": "Alice", "age": 20, "subject": "Physics"}
# Append dictionary to list
students.append(student_info)
print(students)
['Math', 'Science', {'name': 'Alice', 'age': 20, 'subject': 'Physics'}]
Using append() with copy()
When you need to avoid reference issues, use copy() to create a shallow copy before appending ?
import copy
# Create list and dictionary
data_list = []
original_dict = {"city": "Delhi", "population": 30000000}
# Append copy to avoid reference issues
data_list.append(original_dict.copy())
# Modify original dictionary
original_dict["population"] = 32000000
print("List:", data_list)
print("Original dict:", original_dict)
List: [{'city': 'Delhi', 'population': 30000000}]
Original dict: {'city': 'Delhi', 'population': 32000000}
Using append() with deepcopy()
For dictionaries containing nested objects, use deepcopy() to create a complete independent copy ?
import copy
# List to store records
records = []
# Dictionary with nested structure
employee = {
"name": "Bob",
"details": {"department": "IT", "salary": 75000}
}
# Deep copy before appending
records.append(copy.deepcopy(employee))
# Modify original nested dictionary
employee["details"]["salary"] = 80000
print("Records:", records)
print("Original employee:", employee)
Records: [{'name': 'Bob', 'details': {'department': 'IT', 'salary': 75000}}]
Original employee: {'name': 'Bob', 'details': {'department': 'IT', 'salary': 80000}}
Appending Multiple Dictionaries
You can append multiple dictionaries to build a collection of related records ?
# Create empty list for storing multiple records
inventory = []
# Create multiple dictionaries
item1 = {"product": "Laptop", "price": 50000, "stock": 25}
item2 = {"product": "Mouse", "price": 500, "stock": 100}
item3 = {"product": "Keyboard", "price": 1500, "stock": 75}
# Append all dictionaries
inventory.append(item1)
inventory.append(item2)
inventory.append(item3)
print("Inventory:")
for item in inventory:
print(f"- {item['product']}: ?{item['price']} (Stock: {item['stock']})")
Inventory: - Laptop: ?50000 (Stock: 25) - Mouse: ?500 (Stock: 100) - Keyboard: ?1500 (Stock: 75)
Comparison of Methods
| Method | When to Use | Memory Impact |
|---|---|---|
append(dict) |
Simple cases, no modifications | References original |
append(dict.copy()) |
Avoid reference issues | Shallow copy |
append(deepcopy(dict)) |
Nested dictionaries | Complete copy |
Conclusion
Use append() for basic dictionary addition to lists. Choose copy() or deepcopy() when you need independent copies to avoid unintended modifications.
