[Tutor] Select distinct item form list

Gonçalo Rodrigues op73418@mail.telepac.pt
Mon Feb 24 09:34:06 2003


----- Original Message -----
From: <janos.juhasz@VELUX.com>
To: <tutor@python.org>
Sent: Monday, February 24, 2003 7:38 AM
Subject: [Tutor] Select distinct item form list


>Dear All,
>
>Can someone show me a simple list comprehension to do the same thing as
>"Select distinct item form list" does in SQL
>
>So i have a list
>>>> l = (1,2,3,4,5,5,6,7,7,7,2)
>but i would have just
>l=(1,2,3,4,5,6,7)
>
>I know i have seen this somewhere, but i cannot find it :(
>
>Please CC me.
>Best regards,
>-----------------------
>Juhász János
>IT department

Short answer: stick the elements in a dictionary,  call it dct, as in,

for elem in <your_list>:
    dct[elem] = None

then take them out as

unique_elem_list = dct.keys()

and if you want them sorted unique_elem_list.sort().

HTH,
G. Rodrigues