Creating a List of Dictionaries in Python
1. Initializing a List
To create a list of dictionaries in Python, you first need to initialize an empty list using square brackets.
2. Creating Dictionaries
Next, you create multiple dictionaries with the necessary key-value pairs. Each dictionary represents a distinct set of data.
3. Appending Dictionaries to the List
Use the append()
method to add each dictionary to the list.
ls_dict = []
ds1 = {'Name': 'John', 'Age': 25, 'Gender': 'Male'}
ds2 = {'Name': 'Jane', 'Age': 30, 'Gender': 'Female'}
ds3 = {'Name': 'Alex', 'Age': 35, 'Gender': 'Male'}
ls_dict.append(ds1)
ls_dict.append(ds2)
ls_dict.append(ds3)
print(ls_dict)
print(type(ls_dict))
This code snippet demonstrates creating a list of dictionaries, where each dictionary holds information about a person’s name, age, and gender.
Appending a Dictionary to the List of Dictionaries
Appending a new dictionary to an existing list is straightforward. Simply use the append()
method again, providing the new dictionary as the argument.
ds4 = {'Name': 'LinuxforDevices', 'Age': 5, 'Gender': 'Male'}
ls_dict.append(ds4)
Now, ls_dict
contains four dictionaries, including the newly added one.
Accessing Key:Value Pairs from the List of Dictionaries
To access key-value pairs, you can use indexing to target a specific dictionary within the list.
Let’s say you have a list of dictionaries containing information about books:
ls_dict = [{'Title': 'Python for Beginners', 'Author': 'John Doe'}, {'Title': 'Advanced Python Programming', 'Author': 'Jane Smith'}]
1. Accessing a Specific Dictionary
Use its index to retrieve a dictionary.
second_dict = ls_dict[1]
2. Accessing a Value
Use the dictionary’s key to get the corresponding value.
author = second_dict['Author']
print(author)
This will output “Jane Smith,” the author of the second book in the list.
Updating Key:Value Pairs in the List of Dictionaries
You can modify the key-value pairs within a dictionary in the list by adding new pairs, updating existing ones, or deleting pairs.
1. Adding a New Key:Value Pair
Use the update()
method to add a new key-value pair to a dictionary.
ls_dict[0].update({'new_key': 'new_value'})
print(ls_dict[0])
This adds a new key “new_key” with a value of “new_value” to the first dictionary in the list.
2. Updating an Existing Key:Value Pair
Use indexing and the assignment operator to change the value of an existing key.
ls_dict[1]['Title'] = 'Mastering Python Programming'
print(ls_dict[1])
This updates the “Title” key in the second dictionary to “Mastering Python Programming.”
3. Deleting a Key:Value Pair
Use the del
keyword to remove a specific key-value pair from a dictionary.
del ls_dict[0]['Title']
print(ls_dict[0])
This removes the “Title” key and its associated value from the first dictionary.
Conclusion
Creating and manipulating lists of dictionaries is a fundamental skill in Python, especially when working with complex datasets. This article demonstrated the basic operations of creating, appending, accessing, and updating key-value pairs within dictionaries within lists, empowering you to manage and analyze your data effectively.