473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Merging two lists of data (Pythonic way)

SMB
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
Feb 16 '06 #1
9 4552
codes = map(lambda x: x[0], list1)
for d in list2:
if d['code'] in codes:
d['VERIFIED'] = 1

Is this what you were looking for?

Feb 16 '06 #2
SMB

"Jonathan Gardner" <jg******@jonathangardner.net> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
codes = map(lambda x: x[0], list1)
for d in list2:
if d['code'] in codes:
d['VERIFIED'] = 1

Is this what you were looking for?


That is exactly what I was looking for. I will have to take a look at map.

Thank you very much
Feb 16 '06 #3
SMB

"Jonathan Gardner" <jg******@jonathangardner.net> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
codes = map(lambda x: x[0], list1)
for d in list2:
if d['code'] in codes:
d['VERIFIED'] = 1

Is this what you were looking for?


Actually, this is not exactly what I was looking for. I failed to realize
what map did, in this case using the labmda x: x[0] on each item of the list
returning a list of the key numbers. The problem is that the changes made
to my dictionary (in my example dictionary["VERIFIED"]=1) needs to be based
on the other values in each list in LIST1.

In my example LIST1 was defined as
[[5L, 1L, 1L, 'Personal Information'], [14L, 2L, 1L, '']]

So, the dictionary changes would be involving mylist[1:] for each mylist in
LIST1 based on the comparison to mylist[0]...

Thank again
Feb 16 '06 #4
On Thu, 16 Feb 2006 12:51:24 -0800 in comp.lang.python, "SMB"
<se**@buildingonline.com> wrote:

"Jonathan Gardner" <jg******@jonathangardner.net> wrote in message
news:11*********************@g14g2000cwa.googlegr oups.com...
codes = map(lambda x: x[0], list1)
for d in list2:
if d['code'] in codes:
d['VERIFIED'] = 1

Is this what you were looking for?


That is exactly what I was looking for. I will have to take a look at map.


You might also look at list comprehensions. Replacing the first line
in the above with

codes = [x[0] for x in list1]

should yield the same result.

Regards,
-=Dave

--
Change is inevitable, progress is not.
Feb 16 '06 #5
Dave Hansen wrote:
On Thu, 16 Feb 2006 12:51:24 -0800 in comp.lang.python, "SMB"
<se**@buildingonline.com> wrote:

"Jonathan Gardner" <jg******@jonathangardner.net> wrote in message
news:11*********************@g14g2000cwa.googleg roups.com...
codes = map(lambda x: x[0], list1)
for d in list2:
if d['code'] in codes:
d['VERIFIED'] = 1

Is this what you were looking for?


That is exactly what I was looking for. I will have to take a look at map.


You might also look at list comprehensions. Replacing the first line
in the above with

codes = [x[0] for x in list1]

should yield the same result.


Even another way:

import operator
codes = map(operator.itemgetter(0), list1)

Georg
Feb 16 '06 #6
"SMB" <se**@buildingonline.com> writes:
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.
Untested:

from sets import Set
vvals = Set(a[0] for a in LIST1) # get all the List1 first elt. values
for d in LIST2:
if d['code'] in vvals:
d['VERIFIED'] = 1
I know I could do this with two for loops, but am looking for a better
solution maybe involving list comprehension.


I think if list1 is potentially long, the main thing is to avoid n**2
running time, which means use a set or dict to do the lookups, like
above.
Feb 16 '06 #7
Dave Hansen wrote:
On Thu, 16 Feb 2006 12:51:24 -0800 in comp.lang.python, "SMB"
<se**@buildingonline.com> wrote:

"Jonathan Gardner" <jg******@jonathangardner.net> wrote in message
news:11*********************@g14g2000cwa.googlegr oups.com...
codes = map(lambda x: x[0], list1)
for d in list2:
if d['code'] in codes:
d['VERIFIED'] = 1

Is this what you were looking for?


That is exactly what I was looking for. I will have to take a look at map.


You might also look at list comprehensions. Replacing the first line
in the above with

codes = [x[0] for x in list1]

should yield the same result.

Regards,
-=Dave

--
Change is inevitable, progress is not.

What about :

A = [ a[0] for a in LIST1 ]
for d in [ d for d in LIST2 if d['code'] in A ]:
d['VERIFIED'] = 1

?

Gerard

Feb 16 '06 #8
You might consider writing two classes to represent the values in list1
and list2. It might be more Pythonic than sloshing around all of those
lists and dictionaries.

But as it is, it looks like you want to find matching pairs of lists
and dictionaries from list1 and list2, respectively:

# index dictionaries from LIST2 by 'code' element.
# (assumption: no two dicts have the same "code" value)
lookup_table = dict( [(d['code'], d) for list in LIST2] )

# iterate over LIST1, matching list1 elements with corresponding list2
elements
pairs = [( L, lookup_table[L[0]]) for L in LIST1 if L[0] in
lookup_table]

# make whatever kind of modifications you want
for list_item, dictionary_item in pairs:
dictionary_item['VERIFIED'] = list_item[2] # or something


And- If you're using Python 2.4, you can get more efficient memory
usage out of this by using generator expressions instead of list
comprehensions.

Feb 16 '06 #9
On Thu, 16 Feb 2006 12:30:47 -0800, SMB wrote:
I know I could do this with two for loops, but am looking for a better
solution maybe involving list comprehension.


What's wrong with for loops? Why do you think they are unPythonic?
--
Steven.

Feb 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
2
by: Klatuu | last post by:
Whew, I've struggled my way through figuring out how to use XML to transport data..now I can imagine what having a baby is like :) But, I'm stuck now. I generate the XML (single table, no...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
5
by: Jerry Hull | last post by:
I'm working with a database developed by an untrained person over several years - and on a network that has recently been upgraded with a new server installed and MS office upgraded from 2K (I...
15
by: PRadyut | last post by:
In this code it throws a runtime error on a code access violation . On the line z->data=p->data; while (p!=NULL && q!=NULL) { if (*s==NULL) {
1
by: Simon Forman | last post by:
I've got a function that I'd like to improve. It takes a list of lists and a "target" element, and it returns the set of the items in the lists that appear either before or after the target...
11
by: Hakusa | last post by:
Pythonic lists are everything I want in a one dimensional array . . . but I'm trying to do a text adventure and simplify the proces by creating a grid as opposed to a tedious list of rooms this...
11
by: breal | last post by:
I have three lists... for instance a = ; b = ; c = ; I want to take those and end up with all of the combinations they create like the following lists
14
by: etal | last post by:
Here's an algorithm question: How should I efficiently merge a collection of mostly similar lists, with different lengths and arbitrary contents, while eliminating duplicates and preserving order...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.