Transpose list of lists

0 votes
I have the following code :

l = [[1,2,3],[4,5,6],[7,8,9]]
 

The result I'm looking for is

r = [[1,4,7],[2,5,8],[3,6,9]]
 

and not

r = [(1,4,7),(2,5,8),(3,6,9)]

Can anyone help me how to get that?
Mar 1, 2019 in Python by ana1504.k
• 7,890 points
95 views

1 answer to this question.

0 votes
You can try the following and see if it works :

map(list, zip(*l))
--> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
 

For python 3.x users can use :

list(map(list, zip(*l)))
answered Mar 1, 2019 by SDeb
• 13,250 points

Related Questions In Python

0 votes
1 answer

Python: list of lists

Lists are a mutable type - in ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,390 points
73 views
0 votes
1 answer

How to make a flat list out of list of lists?

Hii @kartik, Given a list of lists l, flat_list = ...READ MORE

answered Apr 13 in Python by Niroj
• 23,210 points
29 views
0 votes
1 answer

How to unzip a list of tuples into individual lists?

Hello @kartik, zip is its own inverse! Provided you ...READ MORE

answered Apr 23 in Python by Niroj
• 23,210 points
24 views
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,760 points
194 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 953 views
0 votes
0 answers
0 votes
1 answer

Python: Sort list of lists numerically

You can try and De-dupe it with ...READ MORE

answered May 20, 2019 in Python by SDeb
• 13,250 points
488 views
0 votes
1 answer

Return a list inside a for loop while iterating over the elements of another list

The print() is getting called multiple times ...READ MORE

answered Sep 21, 2018 in Python by SDeb
• 13,250 points
120 views