How do I create a dictionary from 2 different lists?

How to make a dictionary from 2 lists (1 list being the keys and the other values)

The first example I tried to use list comprehensions to create a dictionary from 2 lists, but it’s giving me an error

key = IN[0]
values = IN[1]

result = {key[i] : values[i] for i in range(len(key))}

OUT = result

For the second example, I used defaultdict to try to create a list with repeated keys, but again it returns an error.

key = IN[0]
values = IN[1]

result = defaultdict(list)

for i, j in zip(key, values):
	result[i].append(j)

OUT = result

Both of these examples worked in the online python compiler

I dont think you can have keys with duplicate values.

Thats not the error but i would think thay would be a problem still.

But in the first example, there were no duplicate keys, and it should be a simple dictionary.
Am I doing something really simple wrong?

Your python code is working, you only need string from object after the code block with numbers.

1 Like

Ah thank you so much