Set .discard(), .remove() & .pop()

  • + 1 comment

    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.