List match

thunderfoot at gmail.com thunderfoot at gmail.com
Thu Aug 17 10:15:54 EDT 2006


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']




More information about the Python-list mailing list