Python List of Dictionaries
In Python, a “List” represents a collection of ordered items that are mutable/changeable. On the other hand, a dictionary is a data structure that holds the collection of elements as key-value pairs. Lists are written with square brackets, dictionaries are written with curly braces, and commas separate the items. Keys and values in a dictionary can be of any type.
In Python, a list of dictionaries is a collection of dictionaries where each dictionary represents a single entity, and the list represents a collection of entities.
How to Create/Define a List of Dictionaries?
Python allows us to create a list who’s each item represents a dictionary, and this type of list is referred to as a list of dictionaries. Syntax: To create a list of dictionaries in Python, you can use the following syntax:list_of_dicts = [{'key_1': item_1, 'key_2': item_2, ..., 'key_n': item_n}, {'key_1': item_1, 'key_2': item_2, ..., 'key_n': item_n}, ...]
Let’s comprehend it practically.
Example: Creating a List of Dictionary for Employees’ Data
This example will teach you how to define a list of dictionaries in Python. This example program creates a list of dictionaries for the employee’s data.emp_info = [{1: 'John', 2: 'Sasha', 3: 'Boby'},
{1: 'Alexa', 2: 'Stephan', 3: 'Naomi', 4: 'Jacky'}]
print(emp_info)
The employee id is defined as a key, while the employee name is defined as a value.
Output:
In the above image, two dictionary-type items are created in the “emp_info” list.
How to Access Keys Using Indexes?
To access the keys in a list of dictionaries using indexes, you can use the following syntax:list_of_dict[index][key]
Here is an example of how you can do this.
Example: Accessing Dictionary’s Keys Using Indexes
In this example, some specific keys will be accessed using the indexes:emp_info = [{1: 'John', 2: 'Sasha', 3: 'Boby'},
{1: 'Alexa', 2: 'Stephan', 3: 'Naomi', 4: 'Jacky'}]
print(emp_info[0][2])
print(emp_info[1][3])
Output:
In the above image the second element of the first dictionary and third element of the second dictionary are accessed using the respective indexes.
How to Add/Append a New Dictionary to a List of Dictionaries?
Python’s append() function adds/appends a new dictionary to an already existing list of dictionaries. Syntax: The following syntax is used to append a new dictionary:list.append(new_dictionary)
Note: When you append a dictionary to a list, the dictionary is not copied. Instead, a reference to the same dictionary object is added to the list. So, changing the original dictionary will also affect the list.
Example: Python Code to Append a New Dictionary to a List of Dictionaries
In this example, a new dictionary “new_emp” will be created and appended with an existing list of dictionaries “emp_info”:emp_info = [{1: 'John', 2: 'Sasha', 3: 'Boby'},
{1: 'Alexa', 2: 'Stephan', 3: 'Naomi', 4: 'Jacky'}]
new_emp = {1: 'Jones', 2: 'Trish'}
emp_info.append(new_emp)
print(emp_info)
Output
The “new_emp” dictionary is appended to the “emp_info” list of dictionaries.
How to Delete/Remove a Specific Dictionary From a List of Dictionaries?
In Python, you can delete a particular dictionary using the Del keyword. Syntax The following syntax is used to delete a dictionary from a list of dictionaries:del list_of_dicts[index][key]
Here is an example of how to use the del keyword to delete a dictionary from a list.
Example: Deleting a Dictionary From a List of Dictionaries
Suppose you need to delete the second key of the second dictionary. For this, you can use the del keyword as follows:emp_info = [{1: 'John', 2: 'Sasha', 3: 'Boby'},
{1: 'Alexa', 2: 'Stephan', 3: 'Naomi', 4: 'Jacky'}]
del emp_info[1][2]
print(emp_info)
Output:
The second key of the second dictionary is deleted successfully.