Why do empty sets and lists raise different exceptions when you call .pop()?
>>> l = []
>>> l.pop()
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
l.pop()
IndexError: pop from empty list
>>> l = set()
>>> l.pop()
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
l.pop()
KeyError: 'pop from an empty set'
Because sets
are a lot like dict
s but without the values:
>>> d = {}
>>> d.pop('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'pop(): dictionary is empty'
Both dictionaries and sets are not indexed, like lists are, so an IndexError
makes no sense here. But like dictionaries, there is only ever one value of each 'key' in the set.
Lists are ordered sequences, accessed by index; sets are unordered and non-sequential, accessed by key, hence the error messages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With