I have simplified the original question and hope you can help me a lot
There are 2 lists of equal length:
idlist = [id1,id2,id3,id4,id5]
userlist = [name1,name2,name3,name4,name5]
Hope to merge into a dictionary in the following form:
dict_user = {{'id':id1, 'name':name1},{'id':id2, 'name':name2},{'id':id3, 'name':name3},{' id':id4, 'name':name4},{'id':id5, 'name':name5}}
How to generate the above dictionary?
Supplement: python 3
First of all, your dict_user does not look like a dictionary, but each element in it is a dictionary. You can use the zip function to merge two lists of equal length into a two-dimensional list. In python3, idlist = [id1, id2, id3, id4, id5]
userlist = [name1,name2,name3,name4,name5]
zipped= zip(idlist, userlist)
user= [dict(id=id,name=name) for id ,name in list(zipped)]
You can get
user = [{'id':id1, 'name':name1},{'id':id2, 'name':name2},{'id':id3, 'name':name3},{' id':id4, 'name':name4},{'id':id5, 'name':name5}]
for way
foeEach
First of all
dict_user = { ... } This form should be a set (set/frozenset), but both of them require the stored object to be hashable, that is, it cannot be a variable object like dict, so there is no such thing form.
should be
dict_user = [{'id':id1, 'name':name1},{'id':id2, 'name':name2},{'id':id3, 'name':name3},{ 'id':id4, 'name':name4},{'id':id5, 'name':name5}]
Well, if so, the code is as follows: