- 1
Can Someone Explain to me what the pop() function does to sets
It didn't give an example. Can anyone show please?
5 odpowiedzi
+ 3
It removes and returns an ARBITRARY element from the set. If the set is empty it'd raise a KeyError.
For lists, pop(i) removes and returns the element at index i. If i is not provided, the last element is taken as default. But sets have no index for elements, so a random one is picked.
s = {2, 1, 3}
x = s.pop()
print(x) # 1
print(s) # {2, 3}
Here it appears that 1 is popped every time, but there's no guarantee that it'd be the smallest element (or even a fixed element) for a different set.
+ 2
Oh I didn't know that, I have to downvote my answer 😂
+ 2
This one is to illustrate Kishalaya's pop(i) on list.
https://code.sololearn.com/cFYtswM82xLF/?ref=app
+ 1
Sorry for the late reply. Thanks a lot! It helped!
0
It removes the last entry of the list and store in the variable assigned.
See this demo for you:
https://code.sololearn.com/c4BgVGCuw16N/?ref=app