Python Forum
List Comprehension - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List Comprehension (/thread-7191.html)



List Comprehension - zowhair - Dec-27-2017

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


RE: List Comprehension - metulburr - Dec-27-2017

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)



RE: List Comprehension - zowhair - Dec-29-2017

Thanks man!!