Why doesn’t list have safe “get” method like dictionary?

Why doesn’t list have a safe “get” method like dictionary?

>>> d = {'a':'b'}
>>> d['a']
'b'
>>> d['c']
KeyError: 'c'
>>> d.get('c', 'fail')
'fail'

>>> l = [1]
>>> l[10]
IndexError: list index out of range

13 Answers
13

Leave a Comment