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()
Sort by
recency
|
1045 Discussions
|
Please Login in order to post a comment
This is for discard, pop and remove method. In this method, I have used getattr techniques as it handles the missing attribute and also it returns the value of an attribute.
n = int(input()) s = set(map(int, input().split())) N= int(input()) for i in range(N): output = input().split() if len(output)==1: getattr(s,output[0])() try: if len(output)==2: output1 = getattr(s,output[0]) output1(int(output[1])) except: pass
print(sum(s))
I believe sets do maintain insertion order from Python 3.7 so using
.pop()
will remove the9
which returns an error with.remove(9)
straight afterwards. This challenge might be a bit outdated?The issue is with "pop", it should pop the samllest element from the set but instead it is removing the last element from the set. I just want to understand why it is happening? here is my code
n = int(input()) s = set(map(int, input().split())) N=int(input())
while N>0: C=input()
print(sum(s))
If your solution doesn't work on PyPy3, try using Python 3 and it works. The pop command doesn't remove the same elements in both PyPy3 and Python 3