Python | Zipping two unequal length list in dictionary
Given two lists of possibly unequal lengths, the task is to zip two lists in a dictionary such that the list with shorter length will repeat itself. Since the dictionary in Python is an unordered collection of key:value pairs, the result will be printed on unordered fashion.
Method #1: Using itertools()
Python3
# Python code to demonstrate # return the sum of values of a dictionary # with same keys in the list of dictionary from itertools import cycle # Initialising list of dictionary ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ] ini_lis2 = [ 1 , 2 , 3 ] # zipping in cyclic if shorter length result = dict ( zip (ini_lis1, cycle(ini_lis2))) # printing resultant dictionary print ( "resultant dictionary : " , str (result)) |
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}
Method #2: Using dict comprehension
Python3
# Python code to demonstrate # return the sum of values of dictionary # with same keys in list of dictionary from itertools import cycle # Initialising list of dictionary ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ] ini_lis2 = [ 1 , 2 , 3 ] # zipping in cyclic if shorter length result = {v: ini_lis2[i % len (ini_lis2)] for i, v in enumerate (ini_lis1)} # Printing dictionary print ( "resultant dictionary : " , str (result)) |
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}
Method #3: Using deque()
Python3
# Python code to demonstrate # return the sum of values of dictionary # with same keys in list of dictionary from collections import deque # Initialising list of dictionary ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ] ini_lis2 = deque([ 1 , 2 , 3 ]) # zipping in cyclic if shorter length result = {} for letter in ini_lis1: number = ini_lis2.popleft() result[letter] = number ini_lis2.append(number) print ("resultant dictionary : ", str (result)) |
resultant dictionary : {'c': 3, 'd': 1, 'b': 2, 'e': 2, 'a': 1}
Method 4: Using a for loop and a default dictionary.
- Import defaultdict from the collections module.
- Create an empty defaultdict named ‘result_dict’ with int as the default value type.
- Loop through the ‘ini_lis1’ and ‘ini_lis2’ simultaneously using the built-in zip() function.
- For each iteration, add the value in ‘ini_lis2’ to the corresponding key in ‘result_dict’.
- Return ‘result_dict’.
Python
from collections import deque # Initializing list of dictionary ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ] ini_lis2 = deque([ 1 , 2 , 3 ]) # Zipping in cyclic if shorter length result = {} for letter in ini_lis1: number = ini_lis2.popleft() if letter in result: result[letter] + = number else : result[letter] = number ini_lis2.append(number) # Printing dictionary print ( "Resultant dictionary:" , str (result)) |
('Resultant dictionary:', "{'a': 1, 'c': 3, 'b': 2, 'e': 2, 'd': 1}")
Time complexity: O(n), where n is the length of the longest list (in this case, n = 5).
Auxiliary space: O(n), where n is the number of unique keys in the dictionary. In the worst case, each key has a different value and therefore each key-value pair needs to be stored in the dictionary.
Method #5: Using defaultdict
- A defaultdict is a subclass of the built-in dictionary class. It overrides one method, missing(), which takes a key argument and returns a default value if the key is not present in the dictionary.
- In this case, we can use a defaultdict to automatically create a new key-value pair with a default value of 0 when we encounter a new key.
- We can then simply add the values to the corresponding keys.
Python3
from collections import defaultdict # Initializing list of dictionaries ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ] ini_lis2 = [ 1 , 2 , 3 ] # Creating defaultdict result = defaultdict( int ) # Adding values to corresponding keys for i in range ( len (ini_lis1)): result[ini_lis1[i]] + = ini_lis2[i % len (ini_lis2)] # Printing resultant dictionary print ( "resultant dictionary: " , str ( dict (result))) |
resultant dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 1, 'e': 2}
Time Complexity: O(n), where n is the length of the list of keys (ini_lis1).
Auxiliary Space: O(n)
Method #6: Using zip() and dict()
Python3
# Initializing list of dictionaries ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ] ini_lis2 = [ 1 , 2 , 3 ] # Creating dictionary using zip() result = dict ( zip (ini_lis1, ini_lis2 * (( len (ini_lis1) + len (ini_lis2) - 1 ) / / len (ini_lis2)))) # Printing resultant dictionary print ( "resultant dictionary: " , str (result)) |
resultant dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 1, 'e': 2}
Time Complexity: O(n), where n is the length of the list of keys (ini_lis1).
Auxiliary Space: O(n)
Contact Us