Polat
Polat

Reputation: 57

dictionary changed size during iteration but it didnt?

I'm trying to set the keys in a dictionary from a list and the values from another list . but it's giving me this error and I'm not sure what's wrong :

code = {"" : "" , "" : "" , "" : ""}
keys = ["a" , "b" , "c"]
values = ["z" , "y" , "x"]
for x, k, v in zip(code, keys, values):
  x = k
  code[x] = v
print(code)

Upvotes: 1

Views: 76

Answers (2)

Karan Gavali
Karan Gavali

Reputation: 48

You can try this.

keys = ["a" , "b" , "c"]
values = ["z" , "y" , "x"]
code=dict(zip(keys,values))
print(code)

Upvotes: 1

bottledmind
bottledmind

Reputation: 735

This is the right way of doing what you want

keys = ["a" , "b" , "c"]
values = ["z" , "y" , "x"]
code = {k:v for k,v in zip(keys,values)}

Upvotes: 1

Related Questions