How to unzip a list of tuples into individual lists

0 votes

I have a list of tuples, where I want to unzip this list into two independent lists. I'm looking for some standardized operation in Python.

>>> l = [(1,2), (3,4), (8,9)]
>>> f_xxx (l)
[ [1, 3, 8], [2, 4, 9] ] 

I'm looking for a succinct and pythonic way to achieve this.

Basically, I'm hunting for inverse operation of zip() function.

Apr 23, 2020 in Python by kartik
• 37,510 points
1,605 views

1 answer to this question.

0 votes

Hello @kartik,

zip is its own inverse! Provided you use the special * operator.

>>> zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)])
[('a', 'b', 'c', 'd'), (1, 2, 3, 4)]

The way this works is by calling zip with the arguments:

zip(('a', 1), ('b', 2), ('c', 3), ('d', 4))

except the arguments are passed to zip directly , so there's no need to worry about the number of arguments getting too big.

Hope this work!!

Thank You!!

answered Apr 23, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

How to convert each list item into string in a column of data frame. ?

Hey, To split a string you can use ...READ MORE

answered Feb 5, 2020 in Python by Roshni
• 10,520 points
684 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
3,226 views
0 votes
1 answer

How to sort a list of strings?

Basic answer: mylist = ["b", "C", "A"] mylist.sort() This modifies ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,450 points
1,229 views
0 votes
1 answer

How to sort a list of strings?

Try  items = ["live", "like", "code", "cool", "bug"] ...READ MORE

answered Jul 27, 2018 in Python by Priyaj
• 58,090 points
557 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,023 views
0 votes
1 answer
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
480 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
865 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