I have two lists like these:
a = [12, 23, 45, 56]
b = [0, 0, 0, 0]
And I want to create a dict using a
and b
like this:
c = {12:0, 23:0, 45:0, 56:0}
Is there a easy method to do this?
I have two lists like these:
a = [12, 23, 45, 56]
b = [0, 0, 0, 0]
And I want to create a dict using a
and b
like this:
c = {12:0, 23:0, 45:0, 56:0}
Is there a easy method to do this?
a = [12, 23, 45, 56]
b = [0, 0, 0, 0]
c = dict(zip(a,b))
zip
iterates through your lists in parallel, delivering them in pairs. dict
accepts a sequence of key/value pairs and uses them to make a dictionary.
If you actually want a dictionary where every value is zero, you don't even need the b
list. You can just have
c = {k:0 for k in a}
Just loop through them together:
c = {}
for i,j in zip(a,b):
c[i] = j
Or either a direct usage of zip and dict together:
keys = ()
values = ()
result = dict(zip(keys, values))
# or
result = {k: v for k, v in zip(keys, values)}