+ 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"]

27th Jul 2020, 1:02 PM
Sevda Krithika Sharma
Sevda Krithika Sharma - avatar
4 Answers
+ 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)))}
27th Jul 2020, 1:09 PM
John Robotane
John Robotane - avatar
+ 2
list1=[1,2,3,4,5,7,8] list2=["a","b","c","d","e"] #using zip mydict2=dict(zip(list1,list2)) print(mydict2)
27th Jul 2020, 3:06 PM
Suhail KM
+ 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)))]))
27th Jul 2020, 1:25 PM
Sevda Krithika Sharma
Sevda Krithika Sharma - avatar
+ 1
Got it thanks John Robotane
27th Jul 2020, 1:26 PM
Sevda Krithika Sharma
Sevda Krithika Sharma - avatar