List match

Paddy paddy3118 at netscape.net
Thu Aug 17 17:39:58 EDT 2006


thunderfoot at gmail.com wrote:
> OriginalBrownster wrote:
> > Hi there:
> >
> > I know this probably is a very easy thing to do in python, but i wanted
> > to compare 2 lists and generate a new list that does not copy similar
> > entries. An example below
> >
> > list= ["apple", "banana", "grape"]
> > list2=["orange","banana", "pear"]
> >
> > now I want to compare these lits and generate a third list after
> > comparison
> >
> > list3 would be ["apple", "banana","grape","orange", "pear"]
> >
> > hence the double entry would not show up?
> >
> > Is there a way to do this??
> >
> > Stephen
>
> How about:
>
> list3 = list1 + [item for item in list2 if item not in list1]
>
> print list3:
>
> ['apple', 'banana', 'grape', 'orange', 'pear']

It works with the data given, but if list1 contains duplicates...

- Pad




More information about the Python-list mailing list