[Baypiggies] list question

Shannon -jj Behrens jjinux at gmail.com
Wed Apr 6 21:55:02 CEST 2011


On Tue, Apr 5, 2011 at 7:49 PM, Vikram K <kpguy1975 at gmail.com> wrote:
> i have this list:
> x = ['cat','dog','dog']
>
> i wish to identify the non-unique element in this list i.e. 'dog'. how do i
> do this?
>
> i did this:
>
>>>> y = list(set(x))
>>>> y
> ['dog', 'cat']
>
> but now i don't know to proceed further. have tried the following:
>
>>>> z = [i for i in x if i in y]
>>>> z
> ['cat', 'dog', 'dog']
>
>>>> z = x - y
> Traceback (most recent call last):
>   File "<pyshell#5>", line 1, in <module>
>     z = x - y
> TypeError: unsupported operand type(s) for -: 'list' and 'list'

x = ['cat','dog','dog']
seen = set()
for i in x:
    if i in seen:
        print "dup:", i
    else:
        seen.add(i)

It's O(n).

-- 
In this life we cannot do great things. We can only do small things
with great love. -- Mother Teresa
http://jjinux.blogspot.com/


More information about the Baypiggies mailing list