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

  • + 9 comments

    With getattr, assuming you have loaded the input into a set named s

    for _ in range(N):
        method, *args = input().split()
        getattr(s, method)(*map(int,args))
    

    Of course, that will allow any method. If you want a) only to allow the specified methods and b) slightly faster lookups

    methods = {
        'pop' : s.pop,
        'remove' : s.remove,
        'discard' : s.discard
    }
    for _ in range(N):
        method, *args = input().split()
        methods[method](*map(int,args))