+ 2
Is there any way to create a dictionary from 2 lists without using zip keeping the code maximized to a single line
For eg: List1=[1,2,3,4,5,7,8] List2=["a","b","c","d","e"]
4 Réponses
+ 6
res = [(list1[i],list2[i]) for i in range(min(len(llist1),len(list2)))]
print(list(zip(list1, list2))
print(res)
for a dictionary, I will suppose that the keys are in list1 :
res = { list1[i] : list2[i] for i in range(min(len(llist1),len(list2)))}
+ 2
list1=[1,2,3,4,5,7,8]
list2=["a","b","c","d","e"]
#using zip
mydict2=dict(zip(list1,list2))
print(mydict2)
+ 1
list1=[1,2,3,4,5,7,8]
list2=["a","b","c","d","e"]
print(dict([(list1[i],list2[i]) for i in range(min(len(list1),len(list2)))]))
+ 1
Got it thanks John Robotane