Python Forum
Newbie question: Nested List Comprehensions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie question: Nested List Comprehensions
#1
Looking at this code:

Quote:matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
]
r = [[row[i] for row in matrix] for i in range(4)]
print®

Do not understand how it transposes rows into columns. For a start, does row[1] means [1] or [1,2,3,4]?
If it means 1 then does in each iteration row from "for row" refers to the row below the previous one?
Reply
#2
Understanding a list comprehension may be easier for you if you vreak it into it's elements:
>>> for i in range(4):
...     for row in matrix:
...         print(f'i: {i}, row[i]: {row[i]}')
...
i: 0, row[i]: 1
i: 0, row[i]: 5
i: 0, row[i]: 9
i: 1, row[i]: 2
i: 1, row[i]: 6
i: 1, row[i]: 10
i: 2, row[i]: 3
i: 2, row[i]: 7
i: 2, row[i]: 11
i: 3, row[i]: 4
i: 3, row[i]: 8
i: 3, row[i]: 12
>>>
Reply
#3
Let's unwrap your list comp into standard for loops and add some print statements:
r = []
for i in range(4):
    col = []
    print("i = {}".format(i))
    for row in matrix:
        print("    row = {}".format(row))
        print("    row[{}] = {}\n".format(i, row[i]))
        col.append(row[i])
    r.append(col)

print(r)
Output:
i = 0     row = [1, 2, 3, 4]     row[0] = 1     row = [5, 6, 7, 8]     row[0] = 5     row = [9, 10, 11, 12]     row[0] = 9 i = 1     row = [1, 2, 3, 4]     row[1] = 2     row = [5, 6, 7, 8]     row[1] = 6     row = [9, 10, 11, 12]     row[1] = 10 i = 2     row = [1, 2, 3, 4]     row[2] = 3     row = [5, 6, 7, 8]     row[2] = 7     row = [9, 10, 11, 12]     row[2] = 11 i = 3     row = [1, 2, 3, 4]     row[3] = 4     row = [5, 6, 7, 8]     row[3] = 8     row = [9, 10, 11, 12]     row[3] = 12
i starts at 0 and then we loop throw all the rows and select the 0th element.  Then i becomes 1 and we select the 1th element.  Repeat until i is 4.
Reply
#4
Thank you. What I would like to know now how computer knows to iterate through each row for every i. Why didn't it take only first row for i=0, second row for i=1 etc?
Reply
#5
Because there are two loops.
The outer loop increments i, and for every value of i, the inner loop gets the i-th element of each row.
Reply
#6
Get it, now I see that Larz60+ already presented those two loops in his message.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 691 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 675 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 594 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 964 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  List all possibilities of a nested-list by flattened lists sparkt 1 915 Feb-23-2023, 02:21 PM
Last Post: sparkt
  numpy newbie question bcwilly_ca 4 1,177 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  convert this List Comprehensions to loop jacklee26 8 1,503 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,451 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Updating nested dict list keys tbaror 2 1,275 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,894 Jan-23-2022, 07:20 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020