We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
input().split() returns a sequence. I want method to get the first value in the sequence, args to get the rest. But if I do
method,args=input().split()
args would only get the second value in the list. Putting the * before the name tells Python to give all the remaining items in the sequence into args.
In the second line, the * is doing the reverse. map(int, args) returns a sequence (actually, in Python 3 it's an iterator). But the method we're calling takes multiple arguments, not a single sequence as an argument. So the * tells Python to take all the elements out of the sequence and give them to the method as arguments.
It may be confusing that * is doing the opposite thing in different contexts. I'm afraid Python syntax isn't as consistent as people claim.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Set .discard(), .remove() & .pop()
You are viewing a single comment's thread. Return to all comments →
input().split() returns a sequence. I want method to get the first value in the sequence, args to get the rest. But if I do
args would only get the second value in the list. Putting the * before the name tells Python to give all the remaining items in the sequence into args.
In the second line, the * is doing the reverse. map(int, args) returns a sequence (actually, in Python 3 it's an iterator). But the method we're calling takes multiple arguments, not a single sequence as an argument. So the * tells Python to take all the elements out of the sequence and give them to the method as arguments.
It may be confusing that * is doing the opposite thing in different contexts. I'm afraid Python syntax isn't as consistent as people claim.