Python Append List Repeats Last Element? -
i'm having difficulty figuring out why n-th many lastly elements in "rows" list repeated in item_list list? if rows list of tuples:
(("id 1", "info1", "description1"), ("id 2", "info2", "description2"), ... ("id 14", "info14", "description14"))
and seek create list dictionary out of "rows" list of tuples using next code:
item_list = [] info = {} row in rows: itemid = row[0] iteminfo = row[1] itemdesc = row[2] info['id'] = itemid info['info'] = itemid info['description'] = itemdesc print info item_list.append(info) print item_list
so if "rows" list has 14 elements, i'll 14th element (last element) in "rows" 14 times in "item_list" list of dictionaries.
item_list = [{"id": "id 14", "info": "info14", "description": "description14"}, {"id": "id 14", "info": "info14", "description": "description14"} ... ] #14 times
the "rows" list list of tuples. if print "info" dictionary before appending "item_list" can see each different iteration of "info" dictionary.
anyone have ideas? in advance.
i think want change:
item_list = [] info = {} # <---- move within loop. row in rows: itemid = row[0] itemdesc = row[10]
to:
item_list = [] row in rows: info = {} # <---- here itemid = row[0] itemdesc = row[10]
python list for-loop
No comments:
Post a Comment