Extra list comprehension example

Hello, I was struggling to find an example of list comprehension for a specific situation. The current examples only show if you want to: - Use two loops concurently using non-dependent iterations - Swap values My situation is related to option 1: use one loop that depends on the other loop. I found one example[1] that makes obvious what I want to do (I had swapped the two loops while experimenting): [ expression for line in open('file.txt') for char in line.strip() ] I believe this would be an helpfull example to add to http://docs.python.org/tutorial/datastructures.html#list-comprehensions 1: http://rhodesmill.org/brandon/2009/nested-comprehensions/ -- Met vriendelijke groet, Ruud Althuizen - www.stack.nl - Unix commissie

On Mon 26 Apr 2010 10:42 AM, Ruud Althuizen wrote:
Hello,
I was struggling to find an example of list comprehension for a specific situation. The current examples only show if you want to: - Use two loops concurently using non-dependent iterations - Swap values
My situation is related to option 1: use one loop that depends on the other loop. I just read this line from section 5.1.5: "To avoid apprehension when nesting list comprehensions, read from right to left."
I must've read over that bit the first thousand times. Adding the example probably makes it more obvious. -- Met vriendelijke groet, Ruud Althuizen - www.stack.nl - Unix commissie
participants (1)
-
Ruud Althuizen