Sometimes, while working with lists, we can come forward with a problem in which we need to perform the merge function in which we have the key list and need to create dictionary mapping keys with corresponding value in other list. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using zip() + loop + defaultdict()
The combination of above method can be used to perform this particular task. In this, we use zip function to match the like elements of lists with each other and loop is then used to assign the keys with value from zipped list to add value to a defaultdict.
# Python3 code to demonstrate working of # Merge key value list # Using zip() + loop + defaultdict() from collections import defaultdict # initializing lists test_list1 = [0, 0, 0, 1, 1, 1, 2, 2] test_list2 = ['gfg', 'is', 'best', 'Akash', 'Akshat', 'Nikhil', 'apple', 'grapes'] # printing original lists print("The original list1 is : " + str(test_list1)) print("The original list2 is : " + str(test_list2)) # Using zip() + loop + defaultdict() # Merge key value list res = defaultdict(list) for i, j in zip(test_list1, test_list2): res[i].append(j) # printing result print("The merged key value dictionary is : " + str(dict(res))) |
The original list1 is : [0, 0, 0, 1, 1, 1, 2, 2]
The original list2 is : [‘gfg’, ‘is’, ‘best’, ‘Akash’, ‘Akshat’, ‘Nikhil’, ‘apple’, ‘grapes’]
The merged key value dictionary is : {0: [‘gfg’, ‘is’, ‘best’], 1: [‘Akash’, ‘Akshat’, ‘Nikhil’], 2: [‘apple’, ‘grapes’]}
Method #2 : Using itemgetter() + groupby() + zip()
This is yet another way to perform this particular task. In this, we group the keys fetched by itemgetter to the first list with the elements of other list which is linked using the zip function using groupby method.
# Python3 code to demonstrate working of # Merge key value list # Using itemgetter() + groupby() + zip() from itertools import groupby from operator import itemgetter # initializing lists test_list1 = [0, 0, 0, 1, 1, 1, 2, 2] test_list2 = ['gfg', 'is', 'best', 'Akash', 'Akshat', 'Nikhil', 'apple', 'grapes'] # printing original lists print("The original list1 is : " + str(test_list1)) print("The original list2 is : " + str(test_list2)) # Using itemgetter() + groupby() + zip() # Merge key value list res = {keys: [i for _, i in sub] for keys, sub in groupby( zip(test_list1, test_list2), key = itemgetter(0))} # printing result print("The merged key value dictionary is : " + str(dict(res))) |
The original list1 is : [0, 0, 0, 1, 1, 1, 2, 2]
The original list2 is : [‘gfg’, ‘is’, ‘best’, ‘Akash’, ‘Akshat’, ‘Nikhil’, ‘apple’, ‘grapes’]
The merged key value dictionary is : {0: [‘gfg’, ‘is’, ‘best’], 1: [‘Akash’, ‘Akshat’, ‘Nikhil’], 2: [‘apple’, ‘grapes’]}
Recommended Posts:
- Python | Merge two lists alternatively
- Python | Merge corresponding sublists from two different lists
- Python | Merge two list of lists according to first element
- Python | Merge two lists into list of tuples
- Python | Merge overlapping part of lists
- Python | Merge List with common elements in a List of Lists
- Python | Program to count number of lists in a list of lists
- Python | Zipping two lists of lists
- Python | Merge Python key values to list
- Python | Merge elements of sublists
- Python PIL | Image.merge() method
- Python | Merge two text files
- Python Program for Merge Sort
- Python | List value merge in dictionary
- Python | Merge list elements
- Python - Merge consecutive empty Strings
- Python - Merge List value Keys to Matrix
- Merge two sorted arrays in Python using heapq
- Python | Selective Merge in String list
- Python | Selective Merge list every Nth position
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

