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.
Set .discard(), .remove() & .pop()
Set .discard(), .remove() & .pop()
+ 69 comments n = int(input()) s = set(map(int, input().split())) for i in range(int(input())): eval('s.{0}({1})'.format(*input().split()+[''])) print(sum(s))
+ 19 comments Easy code for beginners Python3
n=int(input())
s = set(map(int,input().split()))
N=int(input())
for i in range(N) :
choice=input().split() if choice[0]=="pop" : s.pop() elif choice[0]=="remove" : s.remove(int(choice[1])) elif choice[0]=="discard" : s.discard(int(choice[1]))
print (sum(s))
+ 3 comments Since the pop operation removes an arbitary element from the set, how do we get the same answer?
+ 7 comments n = int(input()) s = set(map(int, input().split())) t = int(input()) for i in range(t): c, *args = map(str,input().split()) getattr(s,c) (*(int(x) for x in args)) print (sum(s))
+ 2 comments POP is not guaranteed to work as expected, it doesnt POP same element on all python versions. my solution was failing on "pypy2" becasue always last elemnt in set was popped. But same code passed when selected python2.
Load more conversations
Sort 576 Discussions, By:
Please Login in order to post a comment