[Tutor] Yet another list comprehension question

Smith, Jeff jsmith at medplus.com
Fri Mar 2 16:18:18 CET 2007


I find a common thing to do is

l = list()
for i in some-iterator:
    if somefum(i) != list:
    	l.append(somefun(i))

In other words, applying somefun to the results of the iterator return
duplicates but I want the constructed list to contain none.

l = [somefun(i) for i some-iterator]

will produce the redundancies

l = [somefun(i) for i some-iterator if somefun(i) not in l]

doesn't work (not that I expected it to).

Is there a more Pythonic idiom to do this than the for/if block?

Thanks,
Jeff



More information about the Tutor mailing list