keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')
Counting in
dict = {'name' : 'Monty', 'age' : 42, 'food' : 'spam'}
I'm going to make.
Please let me know if you have a better way than I've a code.
keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')
dict = {}
map(lambda k, v: dict.update({k: v}), keys, values)
How about the code below?
First, zip([iterable, ...]) stores i
and tuple
stores the i
th element of each factor
Return list of tuple
.
keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')
myzip = zip(keys,values)
mydict = dict(myzip)
print "---zip(ley, values)---"
print myzip
print "\n---dict(zip(ley, values))---"
print mydict
Results)
---zip(ley, values)---
[('name', 'Monty'), ('age', 42), ('food', 'spam')]
---dict(zip(ley, values))---
{'food': 'spam', 'age': 42, 'name': 'Monty'}
The next method is similar to the structure above
Use less memory than zip()
.
import itertools
keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')
mydict = dict(itertools.izip(keys,values))
© 2024 OneMinuteCode. All rights reserved.