[Tutor] For statement

Sean 'Shaleh' Perry shaleh at speakeasy.net
Sat Mar 13 04:31:09 EST 2004

On Friday 12 March 2004 18:53, Alfred Milgrom wrote:
> You can also specify conditions in your list comprehension expression. For
> example, if you had words with and without commas and only wanted to find
> the words that had a comma at the end, you could write:
> commawords = [word[:-1] for word in a if word.endswith(',')]
> There are many more things you can do with list comprehension, and I have
> simplified the syntax a bit here for ease of explanation, but this should
> give you enough to get started with list comprehension.

as a former C(++) coder one of the things I had to get used to in Python was 
ignoring the details.  The fact that the above is "wasteful" and generates 
another list should just be forgotten.  In languages like python the easiest 
and often fastest approaches are like this.  Don't let yourself get caught up 
with "oh my, I am using twice the memory I should be".  You can always go 
back and optimize the sections that need it.

More information about the Tutor mailing list