0
>>> x = [1,2,3]
>>> y = [4,5,6]
>>> zipped = zip(x,y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> *zipped
  File "<stdin>", line 1
    *zipped
    ^
SyntaxError: invalid syntax
>>> zip(*zipped)
[(1, 2, 3), (4, 5, 6)]  

I am confused with the * before zipped. I understand that zip(*zipped) is used to invert a matrix, but what is the * doing in there? Is it a special operator in python?

hch
  • 6,030
  • 8
  • 20
  • 24

1 Answers1

1

With this:

zip(*zipped)

you tell python the same as this:

zip(zipped[0],zipped[1],zipped[2])

for this basic example.

What does exactly that operator

When used as argument of a function, it takes the elements of the argument and expand it before passing as argument.

For instance:

power = [2,3]
math.pow(*power)

Would give you the value of 2³ = 8.

http://ideone.com/D0R9FB

lilezek
  • 6,976
  • 1
  • 27
  • 45