Merging two lists of data (Pythonic way)

SMB sean at buildingonline.com
Thu Feb 16 15:30:47 EST 2006


I have two lists of data like the following:

LIST1
 [[5L, 1L, 1L, 'Personal Information'], [14L, 2L, 1L, '']]

LIST2
[{'code': 5L, 'name': 'First Name', 'value': [''], 'label': 'First Name', 
'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, 
{'code': 14L, 'name': 'Last Name', 'value': [''], 'label': 'Last Name', 
'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 2L}, 
{'code': 16L, 'name': 'Job Title', 'value': [], 'label': 'Job Title', 
'width': 40L, 'separator': '', 'height': 0L, 'type': 2L, 'order': 3L}]

The important comparison values here are list[0] for each list in LIST1 and 
dictionary["code"] for each dictionary in LIST2.

What I am doing is looking for a pythonic way to parse the LIST2 "code" 
values and make appropriate changes to to each dictionary if the "code" 
value is the first element of a list in LIST1.

The final result may look like this given that the change is adding a new 
key/value of "VERIFIED"/1 for the matches.
LIST3
 [{'VERIFIED':1,'code': 5L, 'name': 'First Name', 'value': [''], 'label': 
'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 
'order': 1L}, {'VERIFIED':1,'code': 14L, 'name': 'Last Name', 'value': [''], 
'label': 'Last Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 
2L, 'order': 2L}, {'code': 16L, 'name': 'Job Title', 'value': [], 'label': 
'Job Title', 'width': 40L, 'separator': '', 'height': 0L, 'type': 2L, 
'order': 3L}]

I know I could do this with two for loops, but am looking for a better 
solution maybe involving list comprehension.

Any pointers are appreciated.
Thanks 





More information about the Python-list mailing list