Python | Zipping two unequal length list in dictionary
Last Updated :
26 Apr, 2023
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
from itertools import cycle
ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ]
ini_lis2 = [ 1 , 2 , 3 ]
result = dict ( zip (ini_lis1, cycle(ini_lis2)))
print ( "resultant dictionary : " , str (result))
|
Output:
resultant dictionary : {'b': 2, 'd': 1, 'c': 3, 'e': 2, 'a': 1}
Method #2: Using dict comprehension
Python3
from itertools import cycle
ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ]
ini_lis2 = [ 1 , 2 , 3 ]
result = {v: ini_lis2[i % len (ini_lis2)]
for i, v in enumerate (ini_lis1)}
print ( "resultant dictionary : " , str (result))
|
Output:
resultant dictionary : {'d': 1, 'c': 3, 'e': 2, 'b': 2, 'a': 1}
Method #3: Using deque()
Python3
from collections import deque
ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ]
ini_lis2 = deque([ 1 , 2 , 3 ])
result = {}
for letter in ini_lis1:
number = ini_lis2.popleft()
result[letter] = number
ini_lis2.append(number)
print ("resultant dictionary : ", str (result))
|
Output:
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
ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ]
ini_lis2 = deque([ 1 , 2 , 3 ])
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)
print ( "Resultant dictionary:" , str (result))
|
Output
('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
ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ]
ini_lis2 = [ 1 , 2 , 3 ]
result = defaultdict( int )
for i in range ( len (ini_lis1)):
result[ini_lis1[i]] + = ini_lis2[i % len (ini_lis2)]
print ( "resultant dictionary: " , str ( dict (result)))
|
Output
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
ini_lis1 = [ 'a' , 'b' , 'c' , 'd' , 'e' ]
ini_lis2 = [ 1 , 2 , 3 ]
result = dict ( zip (ini_lis1, ini_lis2 *
(( len (ini_lis1) + len (ini_lis2) - 1 ) / / len (ini_lis2))))
print ( "resultant dictionary: " , str (result))
|
Output
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)
Share your thoughts in the comments
Please Login to comment...