how to group lists per index in python

Solutions on MaxInterview for how to group lists per index in python by the best coders in the world

showing results for - "how to group lists per index in python"
Lilli
03 Sep 2019
1# initializing the list
2lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
3# empty list
4result = []
5# variable to 0
6index = 0
7# iterating over the sub_list length (3) times
8for i in range(len(lists[0])):
9   # appending an empty sub_list
10   result.append([])
11   # iterating lists length (3) times
12   for j in range(len(lists)):
13      # adding the element to the result
14      result[index].append(lists[j][index])
15# moving to the next index
16index += 1
17# printing the result
18print(result)
similar questions
queries leading to this page
how to group lists per index in python