Misuse of list comprehensions?

Paul McGuire ptmcg at austin.rr.com
Tue May 20 11:58:41 EDT 2008


On May 20, 10:50 am, s0s... at gmail.com wrote:
>
> You don't need all those conditionals. A set differs from a list
> precisely in the fact that each element is unique. And since the
> function is expecting "s" to be an iterable object, it can be
> constructed even without a for loop:
>
> def compress(s):
>     return list(set(s))
>
> That does the trick.
>
>

Only if order does not need to be maintained.  list(set(s)) will not
necessarily keep the unique characters in the order they are seen.
We'll have to check with the OP to see if this is important (I just
assumed that it was because of the use of list comps).

-- Paul



More information about the Python-list mailing list