Asked By – Asher Garland
Wrote this function in python that transposes a matrix:
def transpose(m): height = len(m) width = len(m) return [ [ m[i][j] for i in range(0, height) ] for j in range(0, width) ]
In the process I realized I don’t fully understand how single line nested for loops execute. Please help me understand by answering the following questions:
- What is the order in which this for loop executes?
- If I had a triple nested for loop, what order would it execute?
- What would be equal the equal unnested for loop?
[ function(i,j) for i,j in object ]
- What type must object be in order to use this for loop structure?
- What is the order in which i and j are assigned to elements in object?
- Can it be simulated by a different for loop structure?
- Can this for loop be nested with a similar or different structure for loop? And how would it look?
Additional information is appreciated as well.
Now we will see solution for issue: Single Line Nested For Loops [duplicate]
The best source of information is the official Python tutorial on list comprehensions. List comprehensions are nearly the same as for loops (certainly any list comprehension can be written as a for-loop) but they are often faster than using a for loop.
Look at this longer list comprehension from the tutorial (the
if part filters the comprehension, only parts that pass the if statement are passed into the final part of the list comprehension (here
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
It’s exactly the same as this nested for loop (and, as the tutorial says, note how the order of for and if are the same).
>>> combs =  >>> for x in [1,2,3]: ... for y in [3,1,4]: ... if x != y: ... combs.append((x, y)) ... >>> combs [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
The major difference between a list comprehension and a for loop is that the final part of the for loop (where you do something) comes at the beginning rather than at the end.
On to your questions:
What type must object be in order to use this for loop structure?
An iterable. Any object that can generate a (finite) set of elements. These include any container, lists, sets, generators, etc.
What is the order in which i and j are assigned to elements in object?
They are assigned in exactly the same order as they are generated from each list, as if they were in a nested for loop (for your first comprehension you’d get 1 element for i, then every value from j, 2nd element into i, then every value from j, etc.)
Can it be simulated by a different for loop structure?
Yes, already shown above.
Can this for loop be nested with a similar or different structure for loop? And how would it look?
Sure, but it’s not a great idea. Here, for example, gives you a list of lists of characters:
[[ch for ch in word] for word in ("apple", "banana", "pear", "the", "hello")]
This question is answered By – Jeff Tratner
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0