Sometimes, while working with records we can have problems in which we can have pair of lists, we need to pair similar elements to a single key-value dictionary. This is a very peculiar problem but can have applications in data domains. Let us discuss certain ways in which this task can be performed.
Method #1 : Using loop + extend() + enumerate()
The combination of the above functionalities can be employed to solve this question. In this, we iterate for the lists and append like elements to a similar keys using extend().
# Python3 code to demonstrate working of
# Pair lists elements to Dictionary
# Using loop + extend() + enumerate()
# initializing lists
test_list1 = [1, 2, 3, 1, 1, 2, 3]
test_list2 = [[4, 5], [6, 7], [2, 3], [10, 12],
[56, 43], [98, 100], [0, 13]]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Pair lists elements to Dictionary
# Using loop + extend() + enumerate()
res = dict()
for idx, val in enumerate(test_list1):
if val in res:
res[val].extend(list(test_list2[idx]))
else:
res[val] = list(test_list2[idx])
# printing result
print("The Like elements compiled Dictionary is : " + str(res))
Output
The original list 1 is : [1, 2, 3, 1, 1, 2, 3]
The original list 2 is : [[4, 5], [6, 7], [2, 3], [10, 12], [56, 43], [98, 100], [0, 13]]
The Like elements compiled Dictionary is : {1: [4, 5, 10, 12, 56, 43], 2: [6, 7, 98, 100], 3: [2, 3, 0, 13]}
Time Complexity: O(n*n) where n is the total number of values in the list “test_list”.
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.
Method #2: Using defaultdict() + zip()
The combination of the above tasks can also be used to solve this problem. In this, we pair elements using zip() and initialize the dictionary values as a list to avoid testing for the existence of the first value.
# Python3 code to demonstrate working of
# Pair lists elements to Dictionary
# Using defaultdict() + zip()
from collections import defaultdict
# initializing lists
test_list1 = [1, 2, 3, 1, 1, 2, 3]
test_list2 = [[4, 5], [6, 7], [2, 3], [10, 12],
[56, 43], [98, 100], [0, 13]]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Pair lists elements to Dictionary
# Using defaultdict() + zip()
res = defaultdict(list)
for ele1, ele2 in zip(test_list1, test_list2):
res[ele1].extend(ele2)
# printing result
print("The Like elements compiled Dictionary is : " + str(dict(res)))
Output
The original list 1 is : [1, 2, 3, 1, 1, 2, 3]
The original list 2 is : [[4, 5], [6, 7], [2, 3], [10, 12], [56, 43], [98, 100], [0, 13]]
The Like elements compiled Dictionary is : {1: [4, 5, 10, 12, 56, 43], 2: [6, 7, 98, 100], 3: [2, 3, 0, 13]}
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the using defaultdict() + zip() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #3: Using a dictionary comprehension with list comprehension
Algorithm:
- Zip the two lists together to form a list of tuples.
- Use dictionary comprehension to create a new dictionary, where each key is a unique element from test_list1, and the value is a list of all corresponding elements from test_list2.
- Print the resulting dictionary.
Example:
# Python3 code to demonstrate working of
# Pair lists elements to Dictionary
# Using dictionary comprehension and zip()
# initializing lists
test_list1 = [1, 2, 3, 1, 1, 2, 3]
test_list2 = [[4, 5], [6, 7], [2, 3], [10, 12],
[56, 43], [98, 100], [0, 13]]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Pair lists elements to Dictionary
# Using dictionary comprehension and zip()
res = {key: [val for idx, val in enumerate(
test_list2) if test_list1[idx] == key] for key in set(test_list1)}
# printing result
print("The Like elements compiled Dictionary is : " + str(res))
Output
The original list 1 is : [1, 2, 3, 1, 1, 2, 3]
The original list 2 is : [[4, 5], [6, 7], [2, 3], [10, 12], [56, 43], [98, 100], [0, 13]]
The Like elements compiled Dictionary is : {1: [[4, 5], [10, 12], [56, 43]], 2: [[6, 7], [98, 100]], 3: [[2, 3], [0, 13]]}
Time complexity: O(n^2) where n is the length of the input lists since we are using a nested loop to iterate over both lists.
Auxiliary space: O(n) since we are using a dictionary to store the result, which may contain up to n keys.
Method #4: Using itertools.groupby
In this method, we can use the itertools.groupby function to group the elements of the first list and then create a dictionary using a dictionary comprehension.
from itertools import groupby
# initializing lists
test_list1 = [1, 2, 3, 1, 1, 2, 3]
test_list2 = [[4, 5], [6, 7], [2, 3], [10, 12], [56, 43], [98, 100], [0, 13]]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Pair lists elements to Dictionary
# Using itertools.groupby
res = {k: [sub for _, sub in g] for k, g in groupby(sorted(zip(test_list1, test_list2)), key=lambda x: x[0])}
# printing result
print("The Like elements compiled Dictionary is : " + str(res))
Output
The original list 1 is : [1, 2, 3, 1, 1, 2, 3]
The original list 2 is : [[4, 5], [6, 7], [2, 3], [10, 12], [56, 43], [98, 100], [0, 13]]
The Like elements compiled Dictionary is : {1: [[4, 5], [10, 12], [56, 43]], 2: [[6, 7], [98, 100]], 3: [[0, 13], [2, 3]]}
Time complexity: O(n log n), because we use sorted function
Space complexity: O(n), since we are using a dictionary to store the result, which may contain up to n keys.