Various exceptions for pop from empty sets and lists?

Why do empty sets and lists throw different exceptions when calling .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'
+5
source share
2 answers

Because it is setsvery similar to dict, but without 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, as are lists, so IndexErrorit makes no sense here. But, like dictionaries, in a set there is only one value of each "key".

+7
source

Lists are ordered sequences that are accessed by index; sets are unordered and inconsistent, accessible by key, therefore error messages.

+7
source

All Articles