Python Forum

Full Version: List Comprehension
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
matrix =  [  [ 1,2,3,4], [5,6,7,8],[9,10,11,12]]
[[row[i] for row in matrix] for i in range(4) ]
Will anyone explain these statements for me Idea Idea
[[row[i] for row in matrix] for i in range(4) ]
Please explain this statement in detail
Thanks
if you decompress the list comp to a regular for loop it would look like this. Maybe that would help?
matrix =  [  [ 1,2,3,4], [5,6,7,8],[9,10,11,12]]

lst = []
for i in range(4):
   nested = []
   for row in matrix:
       nested.append(row[i])
   lst.append(nested)
print(lst)
Thanks man!!