Issue
I know that it expands function arguments, but if I try something like this in Python 2:
x = [1,2,3]
print *x # SyntaxError: invalid syntax
print [*x] # SyntaxError: invalid syntax
So it appears that I am missing something about what * exactly does?
Solution
The *
operator, unpacks the elements from a sequence/iterable (for example, list or tuple) as positional arguments to a function
On python2, print
is a statement and not a function. So import print function from future, so that you use *
operator for unpacking a list elements as arguments
>>> from __future__ import print_function
>>> print (*x)
1 2 3
On python3, print
is a function. So you can use *
operator straight-away
>>> print (*x)
1 2 3
Answered By - Sunitha
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.