Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program that renders a dictionary from two list with K values

Improve Article
Save Article
Like Article
  • Last Updated : 13 Mar, 2023
Improve Article
Save Article
Like Article

Given two list, one will be used to provide keys to dictionary and the other for values. The following program takes K values from second list and assigns it to each key, creating a dictionary of the following kind:

Input : test_list = [“gfg”, “best”], val_list = [1, 4, 5, 6, 7, 8, 8, 5], K = 4 
Output : {‘gfg’: [1, 4, 5, 6], ‘best’: [7, 8, 8, 5]} 
Explanation : Equal elements paired to each key.

Input : test_list = [“gfg”], val_list = [1, 4, 5, 6], K = 4 
Output : {‘gfg’: [1, 4, 5, 6]} 
Explanation : All elements assigned to only key. 

Method : Using loop and slicing 

Python3




from collections import defaultdict
 
# initializing list
test_list = ["gfg", "is", "best", "good"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing values list
val_list = [1, 4, 5, 6, 7, 8, 8, 5, 4]
 
# initializing K
K = 2
 
# work list
val_list = val_list[:(len(test_list) * K)]
 
# gets required dictionary list
res = defaultdict(list)
key_cnt = 0
for idx in range(0, len(val_list)):
     
    # append values to required keys   
    res[test_list[key_cnt]].append(val_list[idx])
     
    # increment keys when K
    if (idx + 1) % K == 0:
        key_cnt += 1
 
# printing result
print("The constructed dictionary : " + str(dict(res)))


Output

The original list is : ['gfg', 'is', 'best', 'good']
The constructed dictionary : {'gfg': [1, 4], 'is': [5, 6], 'best': [7, 8], 'good': [8, 5]}

Method #2: Using list comprehension and zip

Approach:

we use a list comprehension and the built-in Python function zip to group the elements in val_list into lists of length K, and then assigns the resulting lists as values for the keys in the dictionary

Algorithm:

1. Initialize an empty dictionary.
2. Calculate the number of slices required based on the length of the val_list and K value.
3. Use list comprehension and zip to group the val_list elements into lists of length K.
4. Assign the resulting lists as values for the keys in the dictionary.
5. Return the resulting dictionary.

Python3




def render_dict(test_list, val_list, K):
    result_dict = {}
    slices = len(val_list) // K + (1 if len(val_list) % K != 0 else 0)
    val_slices = [val_list[i*K:i*K+K] if i*K+K < len(val_list) else val_list[i*K:] for i in range(slices)]
    result_dict = {k: v for k, v in zip(test_list, val_slices)}
    return result_dict
test_list = ["gfg", "is", "best", "good"]
val_list = [1, 4, 5, 6, 7, 8, 8, 5, 4]
K=2
print(render_dict(test_list, val_list, K))


Output

{'gfg': [1, 4], 'is': [5, 6], 'best': [7, 8], 'good': [8, 5]}

Time complexity: O(n), where n is the length of val_list. The list comprehension and zip operations iterate over val_list once.
Space complexity: O(m), where m is the length of test_list. The resulting dictionary has one key-value pair for each element in test_list. Additionally, the val_slices list has length n/K or n/K + 1, depending on the remainder of n/K, which is also bounded by O(m).

 


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!