List match

Richie Hindle richie at entrian.com
Thu Aug 17 10:16:09 EDT 2006


[Stephen]
> [...] 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"]

Use sets:

>>> from sets import Set as set  # For compatibility with Python 2.3
>>> one = ["apple", "banana", "grape"]
>>> two = ["orange","banana", "pear"]
>>> print list(set(one) | set(two))
['grape', 'apple', 'orange', 'pear', 'banana']

-- 
Richie Hindle
richie at entrian.com



More information about the Python-list mailing list