Tuple comprehension

brueckd at tbye.com brueckd at tbye.com
Thu Apr 11 12:38:26 EDT 2002


On Thu, 11 Apr 2002, Sean 'Shaleh' Perry wrote:

> > d = empty list or dict
> > for item in someInputSet:
> >     do something to item and add it to d
> >
>
> since tuples are immutable there is no
> initialize-to-empty-then-loop-to-populate idiom, you are correct.
> Because of this when a tuple would work we have to use lists.
> Numerous times while coding I have started using a tuple and had to
> fall back to lists because I eventually needed a way to give all of
> the values in my list a value based on a function.
>
> I personally like tuples because they say "this list is immutable, it
> won't grow or shrink and try not to touch its contents".  Just like
> using 'const' and 'private' in other languages.

Ah...I think I understand your perspective a little better now. I never
think of a tuple as a list that's immutable, so I don't encounter very
often the same population problem. To me, lists become immutable when I
give them names in all caps, just like my variables become constants when
I name them that way.

-Dave






More information about the Python-list mailing list