[Tutor] Uniques values in a list
Alexandre Ratti
alex@gabuzomeu.net
Sat, 16 Mar 2002 15:32:21 +0100
Hello,
I wanted to filter out unique values in a list. Here is what I found (it is
a frequent question listed in the Python FAQ) :
1) Using lists:
toto = [1, 2, 2, 4]
titi = []
for x in toto:
if x not in titi:
titi.append(x)
print titi
[1, 2, 4]
2) Using dictionaries:
toto = [1, 2, 2, 4]
d = {}
for x in toto:
d[x] = 0
print d.keys()
This solution modifies the list order and only works if the list content
can be used as a dictionary key (eg. is it "hashable").
Source : http://www.lemburg.com/files/python/lists.py.html
=> Do you know any other solution (eg. using functional programming or
similar woodoo)?
Cheers.
Alexandre