0

I have this code and I would like to write it in a list comprehension

list1 = ["A", "B", "C"]
list2 = [1, 2, 3]

my_dict = {}
y = 0
for x in list1:
    my_dict[x] = list2[y]
    y=y+1
print (my_dict)
blhsing
  • 91,368
  • 6
  • 71
  • 106
Sayed Sabry
  • 111
  • 7

2 Answers2

0

You can use the dict constructor with a zip of the two lists instead:

my_dict = dict(zip(list1, list2))
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

Another solution :

my_dict={list1[i]:list2[i] for i in range(len(list1))}
Sruthi
  • 2,908
  • 1
  • 11
  • 25