In programming languages, multiple data structures are available and are used to store large collections of data. Python Lists are linear data structures that store large collections of ordered data. Similarly, in Python, dictionaries are used to store data in “Key-value” pair format.
In this Python guide, the list of dictionaries is created and explained with various examples. In this article, the following topics are discussed:
- How to Create a List of Dictionaries in Python?
- Example 1: Create a List of Dictionaries
- Example 2: Finding the Length of the List of Dictionaries
- Example 3: Acces List of Dictionaries
- Example 4: Update, Add and Delete List of Dictionaries
- Example 5: Appending New List of Dictionaries
Let’s get started!
How to Create a List of Dictionaries in Python?
Before creating a Python List of Dictionaries, the question arises what exactly a Python List of Dictionaries is? The answer is: If the dictionary is initialized as the element of a Python list, then it is called a Python List of Dictionaries.
To create a Python list of dictionaries, all the dictionaries are placed inside the square bracket and separated by commas (,). The syntax is shown below:
Let’s begin with the first example!
Example 1: Create a List of Dictionaries
In the following example, the list of dictionaries is created in Python.
Code:
List_Dict = [{'Name':'Alex','Age':24},
{'weight':52,'Height':6.4},
{'Sex':'Male'},{'Salary':45000}]
print(List_Dict) print(type(List_Dict))
In the above code:
- The list of Dictionaries is initialized in the program.
- For creating a list of dictionaries, the multiple “key-value” pairs are initialized inside the curly braces and enclose all these dictionaries within the square brackets.
- The list of dictionaries is printed on the console output using the print() function.
- Finally, the type() function is utilized with the collaboration of the print() function to print the data type of the list.
Output:
The above output shows that the list of dictionaries has been successfully created.
Example 2: Finding the Length of the List of Dictionaries
The following code snippet utilizes the “len()” function to find the length of the dictionary list:
Code:
List_Dict = [{'Name':'Alex','Age':24},
{'weight':52,'Height':6.4},
{'Sex':'Male'},{'Salary':45000}] print(len(List_Dict))
The above piece of code utilizes the “len()” function to find the total number of dictionaries within the list.
Output:
The output shows four dictionaries in the given list, i.e., “List_Dict”.
Example 3: Acces List of Dictionaries
In the list of dictionaries, the “key” value and the whole dictionary list can be accessed using the index number and the name of the key. The below example code will show how we can access a list of dictionaries.
Code:
List_Dict = [{'Name':'Alex','Age':24},
{'weight':52,'Height':6.4},
{'Sex':'Male'},{'Salary':45000}]
print(List_Dict[0],List_Dict[1],List_Dict[2])
print(List_Dict[3])
print(List_Dict[1]['Height'])
In the mentioned code:
- The list of dictionaries is initialized in a variable named “List_Dict ”.
- The dictionary list can be accessed using the name of the initialized variable and index number such as “List_Dict[0]”. Here, the value “0” indicates the index number of the first list of dictionaries.
- The index numbers “1 and 2” are passed to access the second and third dictionaries of the list, respectively.
- From the dictionary list, a specific “Key” value can be accessed by passing the dictionary’s index number and name.
- In the above code, “List_Dict[1][‘Height’]” is used to access the value of the “Height” key from the second dictionary.
Output:
The above output shows that the list of dictionaries is accessed using the index number and key name.
Example 4: Update, Add and Delete List of Dictionaries
We can add, update and delete the “key-value” pair within the existing “list of dictionaries” using the index number and key name. The “del” statement is used here to remove a specific key-value pair. The following example will show the concept of updating, adding and removing key value:
Code:
List_Dict = [{'Name':'Alex','Age':24},
{'weight':52,'Height':6.4},
{'Sex':'Male'},{'Salary':45000}]
#updating value
List_Dict[0]['Age'] = 12
#adding a new key:value pair
List_Dict[0]['Name_1'] = 'David'
#delete a key:value pair
del List_Dict[3]['Salary']
print(List_Dict)
In the above code:
- The list of dictionaries having length “4” is initialized in the program.
- “List_Dict[0][‘Age’]” was assigned with a new value “12”. Here, “0” represents the first dictionary of the list, while “Age” represents a key to be updated.
- Next, we assigned “David” to the “List_Dict[0][‘Name_1’],” which states that add a new key named “Name_1” in the first list and assign it a value “David”.
- To delete the specific dictionary from a list, the prefix “del” is used. The index number and key value of the particular dictionary are mentioned in the statement “del List_Dict[3][‘Salary’]”.
Output:
The above output shows the List of Dictionaries in which the following operations are performed: updating, adding, and removing key-value pairs.
Example 5: Appending New List of Dictionaries
The inbuilt function “append()” is used to add the list of dictionaries into the existing list of dictionaries. Let’s understand via the following example:
Code:
List_Dict = [{'Name':'Alex','Age':24},
{'weight':52,'Height':6.4},
{'Sex':'Male'},{'Salary':45000}]
List_Dict.append({'Designation':'Junior Developer',
'Services':'Web developing'})
print(List_Dict)
In the above code:
- The list of dictionaries having a length “4” is initialized in the program.
- A new dictionary is appended in the same list, i.e., “List_Dict”. For this purpose, the “append()” function is used.
Output:
The above output shows that a new dictionary is appended to the given list using the “append()” function.
That’s all from this Python Tutorial!
Conclusion
To create a Python list of dictionaries, place all the dictionaries within the square bracket using the comma-separated syntax. Every dictionary is an element of a list. The list of dictionaries and the key value of particular dictionaries can be accessed using the index number. The “append()” function adds a new dictionary as an element of the list. This article provides a detailed overview of the Python List of Dictionaries.