Issue 32979: dict get() function equivalent for lists. (original) (raw)
Hi there!
I hope this wasn't suggested before. I couldn't find any issues related to it.
The get()
function on the dictionary object is such a convenient way for retrieving items from a dict that might not exists. I always wondered why the list object does not have an equivalent?
I constantly run into something like this:
myval = mylist[1] if len(mylist) > 1 else None
or worse like this:
try:
myval = mylist[1]
except IndexError:
myval = None
While I think it would be nice to do it like this:
myval = mylist.get(1)
Any love for this?
Cheers! :)