beginner, idiomatic python

bambam david at asdf.asdf
Fri Aug 24 05:07:48 EDT 2007


> 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.

oh :~). Learning experience happening here... Thank you.

> The reason that lists don't have set-like methods is because
> lists aren't sets -- lists can contain duplicate elements

Interesting point -- if that's all there is in it, then lists should
have difference and intersection methods. Not because they
are the same as sets -- because they are slightly different than
sets. In this case it doesn't matter - my lists don't contain
duplicate elements this time - but I have worked with lists in
money market and in inventory, and finding the intersection
and difference for matching off and netting out are standard
operations.
Still, any built in feature would probably be too simple to
use in any but the simplest cases.

Steve.



"Erik Max Francis" <max at alcyone.com> wrote in message 
news:CqSdnRl2x8928FPbnZ2dnUVZ_jSdnZ2d at speakeasy.net...
> 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