Merging two lists of data (Pythonic way)

Gerard Flanagan grflanagan at yahoo.co.uk
Thu Feb 16 16:19:24 EST 2006


Dave Hansen wrote:
> On Thu, 16 Feb 2006 12:51:24 -0800 in comp.lang.python, "SMB"
> <sean at buildingonline.com> wrote:
>
> >
> >"Jonathan Gardner" <jgardner at jonathangardner.net> wrote in message
> >news:1140122391.823206.74580 at g14g2000cwa.googlegroups.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




More information about the Python-list mailing list