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

Sort by

recency

|

1056 Discussions

|

  • + 0 comments
    n = int(input())
    s = set(map(int, input().split()))
    N = int(input())
    
    for i in range(N):
        command = input().split()
        try:
            if len(command) == 2:
                getattr(s,command[0])(int(command[1]))
            else:
                getattr(s,command[0])() 
        except KeyError:
            pass
        except Exception:
            pass
    
    print(sum(s))
    
  • + 0 comments

    WHY NOT WORKING? n = int(input()) st = [] for i in range(n): st.extend(map(int, input().split()))

    o = int(input()) for _ in range(o): oper = input().split() if oper[0] == 'pop': if st:
    st.pop() elif oper[0] == 'remove': y = int(oper[1]) if y in st: st.remove(y) elif oper[0] == 'discard': y = int(oper[1]) if y in st: st.remove(y)

    print(st)

  • + 0 comments
    n = int(input())
    s = set(map(int, input().split()))
    N = int(input())
    
    for _ in range(N):
        command_list = list(map(str, input().split()))
        if command_list[0].lower() == 'pop':
            s.pop()
        elif command_list[0].lower() == 'discard':
            s.discard(int(command_list[1]))
        elif command_list[0].lower() == 'remove':
            try:
                s.remove(int(command_list[1]))
            except None:
                pass
    print(sum(s))
    
  • + 0 comments

    If anyone is hitting a snag using PyPy, switch to Python3. There is some deterministic way that pop removes items from the set that breaks subsequent calls to remove().

  • + 1 comment

    If anyone is hitting a snag using PyPy, switch to Python3. There is some deterministic way that pop removes items from the set that breaks subsequent calls to remove().

    • + 0 comments

      Thanks