pyeR_biz
pyeR_biz

Reputation: 1044

Create dict from two lists, with 1:2 key:value relation

How do I create a dictionary from below lists, len(keys_list) = 12, len(values_list) = 24.

keys_list = ['Al','Bb','Ch','Da','Ev','Fr','Gi','Ht','Ia','Jh','Kd','Ly']
values_list = [i for i in 'VRCGVVRVCGGCCGVRGCVCGCGV']

So my output will be ['Al':'VR' or ('V','R') or ['V','R']......] either way is fine. I tried a few variations of zip(); and reached this post where they used zip_longest.

import itertools
for i in itertools.zip_longest(keys_list,values_list):
    print (i)

I prefer not having to import a module.

Upvotes: 0

Views: 52

Answers (1)

shahaf
shahaf

Reputation: 4973

keys_list = ['Al','Bb','Ch','Da','Ev','Fr','Gi','Ht','Ia','Jh','Kd','Ly']
values_list = [i for i in 'VRCGVVRVCGGCCGVRGCVCGCGV']

grouped_values = [values_list[i] + values_list[i+1] for i in range(len(values_list) - 1)]
d = {k :v for k,v in zip(keys_list, grouped_values)}
print(d)

output

{'Al': 'VR', 'Bb': 'RC', 'Ch': 'CG', 'Da': 'GV', 'Ev': 'VV', 'Fr': 'VR', 'Gi': 'RV', 'Ht': 'VC', 'Ia': 'CG', 'Jh': 'GG', 'Kd': 'GC', 'Ly': 'CC'}

Upvotes: 1

Related Questions