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

  • + 0 comments

    I believe sets do maintain insertion order from Python 3.7 so using .pop() will remove the 9 which returns an error with .remove(9) straight afterwards. This challenge might be a bit outdated?

    n = int(input())
    s = set(map(int, input().split()))
    N = int(input())
    for _ in range(N):
        command = input()
        if command == "pop":
            s.pop()
            continue
        word, value = command.split()
        if word == "remove":
            s.remove(int(value))
        elif word == "discard":
            s.discard(int(value))
    print(sum(s))