Merging two list of dictionaries in Python


Problematic data structures include Python lists and dictionaries, these two structures being ordered collections that allow changes, while an unordered dictionary stores values like maps. Dictionaries provide more efficient use by holding key-value pairs; unlike other data types which hold single values at a time as elements in them.

In this article, merging two lists involve compiling all dictionaries from both lists into one large list, their order in this merged list will mirror how they appeared in their respective original lists, initially the order may contain only those from one original list followed by others from both original lists. We create a Python solution that merges two separate lists of dictionaries into a single, more manageable list - making subsequent data processing and analysis tasks more effective and efficient.

Merging two Lists of Dictionaries in Python Using ‘+’ Operator

Code Explanation and Design Steps −

  • Step 1 − Open Jupyter Notebook in Anaconda prompt and start writing the code in its cell.

  • Step 2 − Initializing two lists of Dictionaries, we define our input as two ‘Lists’; these ‘Lists’ each contain multiple Dictionaries that contain ‘key-value pairs’ as ‘keys’ and ‘values’ respectively.

  • Step 3 − Merge two lists together: Once we prepare the input, merge both lists together into one. Concatenating both lists into a single one using ‘+’, not altering their original versions but creating one new list combining their elements together into a whole.

  • Step 4 − Assign the results to variables.

  • Step 5 − Verify results.

The final output should consist of one list containing all dictionaries from both input lists in their original order; every dictionary remains independent with all key-value pairs maintained as per the original lists.

Keep in mind that this operation does not modify the original list nor merge dictionaries with identical keys; rather, it merges both lists into one new list where every dictionary, regardless of key, remains a standalone element.

Example 1

Code for merging two lists of dictionaries in python using ‘+’ operator −

# Merging two lists of dictionaries in Python using ‘+’ operator 
# Sample lists of dictionaries
# Declare the two lists of dictionaries
list1 = [{'name': 'Sarita', 'age': 25}, {'name': 'Alice', 'age': 30}]
list2 = [{'name': 'Bob', 'age': 35}, {'name': 'Sara', 'age': 28}]

# Combine the two lists
merged_list = list1 + list2

# Print the merged list
print(merged_list)

Output

[{'name': 'Sarita', 'age': 25}, {'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 35}, {'name': 'Sara', 'age': 28}]

Merging two List of Dictionaries in Python Using ‘extend()’ Method

Code Explanation and Design Steps −

  • Step 1 − Open Jupyter Notebook in Anaconda prompt and start writing the code in its cell.

  • Step 2 − Establish Lists of Dictionaries named list1 and list2.

  • Step 3 − Use the ‘extend ()’ method to merge lists.

  • Step 4 − After calling the ‘extend()’ method, ‘list1’ becomes a combined list that contains dictionary items from both original lists. Print ‘list1’ to verify its results on the console.

Example 2

Code for Merging two List of Dictionaries in Python Using ‘extend()’ Method −

# Merging two list of dictionaries in Python using ‘+’ operator 
# Sample lists of dictionaries
# Declare the two lists of dictionaries
list1 = [{'name': 'Sarita', ' salary ': 55000}, {'name': 'Alice', ' salary ': 30000}]
list2 = [{'name': 'Bob', ' salary ': 35000}, {'name': 'Sara', ' salary ': 28000}]

# Combine the two lists
merged_list = list1.copy()  # Make a copy of the first list to preserve its 
   #  original content
merged_list.extend(list2)  # Add elements from the second list to the  
   #  merged_list
# Print the merged list
print(merged_list)

Output

[{'name': 'Sarita', 'age': 25}, {'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 35}, {'name': 'Sara', 'age': 28}]

Conclusion

In this article, we are merging two lists of dictionaries in Python using two different methods as ‘+’ operator and extend() method. Furthermore, data cleansing and consolidation is an integral component of many data manipulation and data wrangling tasks, enabling the integration of disparate sources' data sources into an easily usable format that facilitates analysis and processing tasks.

Updated on: 18-Oct-2023

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements