beginner, idiomatic python
Erik Max Francis
max at alcyone.com
Fri Aug 24 01:34:32 EDT 2007
bambam wrote:
> Excellent. By symmetry, I see that "list" casts the set back into a list.
>
> I wonder why list has not been extended with the same (difference,
> interesection) methods? Casting to set looks a little kludgy:
>
> c = list(set(a)-set(b))
> I wonder if that is clearer than the explicit loop?
This isn't a "cast" in the sense of some less-strongly-typed languages;
it's just a conversion. The `list` function/type iterates over its
argument and turns it into a list. Sets are iterable, so that's all
that's really going on here.
The reason that lists don't have set-like methods is because lists
aren't sets -- lists can contain duplicate elements, whereas sets
cannot. You should use the proper type for your needs; if you want to
take two lists, remove duplicate elements, and then end up with a list,
then the sets-difference-and-then-make-a-list mechanism is appropriate.
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
It [freedom] must be demanded by the oppressed.
-- Dr. Martin Luther King, Jr.
More information about the Python-list
mailing list