Exception Handling for lists and sets¶
In [1]:
l = [1, 2, 3, 4]
In [2]:
l[4]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /tmp/ipykernel_99/3011213518.py in <module> ----> 1 l[4] IndexError: list index out of range
In [3]:
l.pop(4)
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /tmp/ipykernel_99/4109986001.py in <module> ----> 1 l.pop(4) IndexError: pop index out of range
In [4]:
s = {1, 2, 3}
In [8]:
s.remove(4)
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /tmp/ipykernel_99/3348318046.py in <module> ----> 1 s.remove(4) KeyError: 4
In [12]:
s.pop()
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /tmp/ipykernel_99/3455620568.py in <module> ----> 1 s.pop() KeyError: 'pop from an empty set'