Why does x y zip zip a b work in Python

+1 vote

OK I love Python's zip() function. Use it all the time, it's brilliant. Every now and again I want to do the opposite of zip(), think "I used to know how to do that", then google python unzip, then remember that one uses this magical * to unzip a zipped list of tuples. Like this:

x = [1,2,3] y = [4,5,6] zipped = zip(x,y) unzipped_x, unzipped_y = zip(*zipped) unzipped_x Out[30]: (1, 2, 3) unzipped_y Out[31]: (4, 5, 6)

What on earth is going on? What is that magical asterisk doing? Where else can it be applied and what other amazing awesome things in Python are so mysterious and hard to google?

Aug 23, 2018 in Python by bug_seeker
• 15,520 points
1,573 views

1 answer to this question.

0 votes

I'm extremely new to Python so this just recently tripped me up, but it had to do more with how the example was presented and what was emphasized.

What gave me problems with understanding the zip example was the asymmetry in the handling of the zip call return value(s). That is, when zip is called the first time, the return value is assigned to a single variable, thereby creating a list reference (containing the created tuple list). In the second call, it's leveraging Python's ability to automatically unpack a list (or collection?) return value into multiple variable references, each reference being the individual tuple. If someone isn't familiar with how that works in Python, it makes it easier to get lost as to what's actually happening.

>>> x = [1, 2, 3]
>>> y = "abc"
>>> zipped = zip(x, y)
>>> zipped [(1, 'a'), (2, 'b'), (3, 'c')]
>>> z1, z2, z3 = zip(x, y)
>>> z1 (1, 'a')
>>> z2 (2, 'b')
>>> z3 (3, 'c')
>>> rezipped = zip(*zipped)
>>> rezipped [(1, 2, 3), ('a', 'b', 'c')]
>>> rezipped2 = zip(z1, z2, z3)
>>> rezipped == rezipped2 
True
answered Aug 23, 2018 by Priyaj
• 58,100 points

Related Questions In Python

0 votes
1 answer

return [x + y for x, y in zip(a, b)]

Hello @Ahmad, Could you please explain your workaround ...READ MORE

answered Nov 3, 2020 in Python by Gitika
• 65,890 points
756 views
0 votes
1 answer

How does Python know whether a variable in the class is a method or a variable?

In python objects/variables are wrapped into methods ...READ MORE

answered Sep 18, 2018 in Python by aryya
• 7,460 points
1,111 views
0 votes
1 answer

How does insertion work in Python?

Or, this one: def ins_sort(k): ...READ MORE

answered Oct 8, 2018 in Python by charlie_brown
• 7,720 points
778 views
0 votes
1 answer

How does % work in Python?

The % (modulo) operator yields the remainder ...READ MORE

answered Oct 10, 2018 in Python by SDeb
• 13,300 points
767 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,348 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,904 views
0 votes
1 answer

How does slice notation in Python work?

The Python tutorial talks about it (scroll down a ...READ MORE

answered Oct 31, 2018 in Python by Priyaj
• 58,100 points
708 views
0 votes
1 answer

How do I copy a file in python?

Use the shutil module. copyfile(src, dst) Copy the contents ...READ MORE

answered Jul 31, 2018 in Python by Priyaj
• 58,100 points
924 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