Answer the question
In order to leave comments, you need to log in
How to merge two lists into a dictionary?
There are two lists, for example list 1:
'6.95',
'6.95',
'6.95',
'6.95',
'6.96',
'6.96',
'6.96',
'6.96',
'6.96',
'6.96',
'6.96',
'6.96',
'6.96',
'6.96',
'6.96',
'6.96',
'0/4',
'0/10',
'0/6',
'0/18',
'0/9',
'0/24',
'0/22',
'0/11',
'0/14',
'0/7',
dict[str(list1)] = str(list2)
'6.95': '0/4', '0/10', '0/6', '0/18','0/9','0/24'
'6.96'; '0/22','0/11', '0/14','0/7',
'6.95': '0/4',
'6.95': '0/10',
'6.95': '0/18'
'6.96': '0/22',
'6.96': '0/11'
и тд
Answer the question
In order to leave comments, you need to log in
A variant for the case when for non-repeating keys the values do not need to be wrapped in a list:
d = {}
for k, v in zip(list1, list2):
if k in d:
if not isinstance(d[k], list):
d[k], t = [], d[k]
d[k].append(t)
d[k].append(v)
else:
d[k] = v
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question