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,910 points
963 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 2, 2019 by SDeb
• 13,300 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,520 points
462 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, 2020 in Python by Niroj
• 82,880 points
488 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, 2020 in Python by Niroj
• 82,880 points
1,615 views
0 votes
1 answer

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

Hello @kartik, You can use itertools.chain(): import itertools list2d = [[1,2,3], ...READ MORE

answered Jun 5, 2020 in Python by Niroj
• 82,880 points
872 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,060 views
0 votes
1 answer
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,300 points
2,017 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 22, 2018 in Python by SDeb
• 13,300 points
4,676 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP