[Tutor] finding non unique data in lists
kent_johnson at skillsoft.com
Thu Oct 14 13:44:55 CEST 2004
You can make a Set from each list, then find the intersection of all the
sets. That will contain just the elements in all the lists.
>>> from sets import Set
First some raw data:
>>> l1 = [1,2,3,4,5,6,7]
>>> l2 = [2,3,4,7]
>>> l3 = [1,2,3,5,6,7]
Sets can be constructed from lists:
>>> s1 = Set(l1)
Set([1, 2, 3, 4, 5, 6, 7])
>>> s2 = Set(l2)
>>> s3 = Set(l3)
Sets overload the & operator to mean set intersection:
>>> all = s1 & s2 & s3
Set([2, 3, 7])
Sets allow iteration to process their elements one-by-one:
>>> for i in all:
... print i
For more information about sets see the library reference page at
At 01:12 PM 10/14/2004 +0200, Scott Melnyk wrote:
>I have a collection of lists of different lengths (containing exons
>from genetic data) that I would like to compare and only keep the
>those that are found within each of the lists. Any exon not present
>in all the lists is not important to me.
>I am new to python and was told that sets could be used easily to find
>the unique data but I am unsure the most efficient way how to pull out
>the non-unique data.
>All suggestions are very much appreciated and welcome.
>Tutor maillist - Tutor at python.org
More information about the Tutor