On 2021-07-06 02:52, Austin Graham wrote:
I'm a lurker on this list and wanted to submit my own idea. It's a
feature I've been wanting in Python for a while, and I have a simple
implementation for it for personal use at the moment. Wanted to get
feedback before I move forward.
Essentially, I want to be able to perform a simple default get on a list
if an index does not exist, similar to a dictionary. For example:
*my_dict = {"test1": "value1"}*
*my_dict.get("test2", None)*
*
*
results in a "None" value, since the key doesn't exist.
Similarly with a list:
*my_list = [1, 2, 3]*
*my_list.get(10, 0)*
*
*
says that since index 10 does not exist, I expect a 0 to be returned.
Currently, you have to check the length of the list of use a try/except,
which takes more lines than I'd like to use.
I'm welcoming feedback on this feature, or if it's already been
discussed then I'm expecting to be shut down.
Thank you for your time!
This has been discussed before, most recently on 2021-06-05, but also in
August 2020, and in 2017.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/2RDSMGNBVSSU5T323WPX4DL3LUZAMWQO/
Code of Conduct: http://python.org/psf/codeofconduct/