* operator in python tutorial

B

ben

Hello,

I am following through the python tutorial which gets to a line that
uses the * operator with zip(). I searched and searched but could find
no information on the operator or how to use it in general. The
example from the tut is as follows:
x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)
zipped [(1, 4), (2, 5), (3, 6)]
x2, y2 = zip(*zipped)
x == list(x2) and y == list(y2)
True

How would i apply the * operator in general?
Thanks.
 
B

ben

I missed it because I skipped to the explanation of zip. I hit the
back arrow and went on and saw the link to "Unpacking Argument Lists"
which explained what I needed to know.

Thanks.
 
G

Gringo

Hello,

I am following through the python tutorial which gets to a line that
uses the * operator with zip(). I searched and searched but could find
no information on the operator or how to use it in general. The
example from the tut is as follows:
x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)
zipped [(1, 4), (2, 5), (3, 6)]
x2, y2 = zip(*zipped)
x == list(x2) and y == list(y2)
True

How would i apply the * operator in general?
Thanks.

The * operator, when used in this context, unpacks the sequence and it's
as if you passed each item to the function as a different parameter.
For example, if you have a list x with 4 items, these two statements
would be the same:
f(x[0],x[1],x[2],x[3])
f(*x)

Hope this helps.
 
B

ben

I am following through the python tutorial which gets to a line that
uses the * operator with zip(). I searched and searched but could find
no information on the operator or how to use it in general. The
example from the tut is as follows:
x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)
zipped [(1, 4), (2, 5), (3, 6)]
x2, y2 = zip(*zipped)
x == list(x2) and y == list(y2) True

How would i apply the * operator in general?
Thanks.

The * operator, when used in this context, unpacks the sequence and it's
as if you passed each item to the function as a different parameter.
For example, if you have a list x with 4 items, these two statements
would be the same:
f(x[0],x[1],x[2],x[3])
f(*x)

Hope this helps.

It does.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top