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 in Python by ana1504.k
• 7,430 points
20 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 by SDeb
• 12,280 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,310 points
29 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,710 points
43 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
146 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
544 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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 124 views
0 votes
1 answer

Python: Sort list of lists numerically

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

answered May 20 in Python by SDeb
• 12,280 points
33 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
• 12,280 points
41 views