- 1

Can Someone Explain to me what the pop() function does to sets

It didn't give an example. Can anyone show please?

18th Dec 2018, 2:42 AM
BitsAndBytes
BitsAndBytes - avatar
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.
18th Dec 2018, 4:18 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
Oh I didn't know that, I have to downvote my answer 😂
18th Dec 2018, 4:20 AM
Gordon
Gordon - avatar
+ 2
This one is to illustrate Kishalaya's pop(i) on list. https://code.sololearn.com/cFYtswM82xLF/?ref=app
18th Dec 2018, 4:28 AM
Gordon
Gordon - avatar
+ 1
Sorry for the late reply. Thanks a lot! It helped!
23rd Dec 2018, 5:08 PM
BitsAndBytes
BitsAndBytes - avatar
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
18th Dec 2018, 3:27 AM
Gordon
Gordon - avatar