Ultimately it probably doesn't have a safe .get method because a dict is an associative collection (values are associated with names) where it is inefficient to check if a key is present (and return its value) without throwing an exception, while it is super trivial to avoid exceptions accessing list elements (as the len method is very fast). The .get method allows you to query the value associated with a name, not directly access the 37th item in the dictionary (which would be more like what you're asking of your list).

Of course, you can easily implement this yourself:

def safe_list_get (l, idx, default):
  try:
    return l[idx]
  except IndexError:
    return default

You could even monkeypatch it onto the __builtins__.list constructor in __main__, but that would be a less pervasive change since most code doesn't use it. If you just wanted to use this with lists created by your own code you could simply subclass list and add the get method.

Answer from Nick Bastin on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ why no .get(idx[, default]) on python list??
r/Python on Reddit: Why no .get(idx[, default]) on python list??
June 23, 2022 -

Hi all,

today it happened once again that I could really use a .get(idx[, default]) method on python lists. Here is a brief example why it could be useful (I know there are many alternative solutions to this specific problem here, so please focus generally on the idea of .get for lists).

file_name = 'test.png'
if '.' in file_name:
    extension = file_name.rsplit('.', maxsplit=1)[1]
else:
    extension = ''

If we had such a method we could make the code much more concise

file_name = 'test.png'
extension = file_name.rsplit('.', maxsplit=1).get(1, '')

I wonder why this useful method does not exist, especially since it is available for dicts.

dd = {'a': 'AAA'}
print(f"{dd['a']}; {dd.get('a')}; {dd.get('c')}; {dd.get('c', 'nothing here')}; ")
# AAA; AAA; None; nothing here;

Thoughts / ideas why this is not present? Are there valid reasons not to have this method? Is it not available because someone has to invest the work to code it? How could something like this be initiated? :)

Top answer
1 of 23
155
There was a lengthy thread on this on Python-Ideas a couple of years ago: https://mail.python.org/archives/list/python-ideas@python.org/thread/LLK3EQ3QWNDB54SEBKJ4XEV4LXP5HVJS/ The clearest explanation of the most common objection was from Marc-Andre Lemburg: dict.get() was added since the lookup is expensive and you want to avoid having to do this twice in the common case where the element does exist. It was not added as a way to hide away an exception, but instead to bypass having to generate this exception in the first place. dict.setdefault() has a similar motivation. list.get() merely safes you a line of code (or perhaps a few more depending on how you format things), hiding away an exception in case the requested index does not exist. If that's all you want, you're better off writing a helper which hides the exception for you. I argue that making it explicit that you're expecting two (or more) different list lengths in your code results in more intuitive and maintainable code, rather than catching IndexErrors (regardless of whether you hide them in a method, a helper, or handle them directly). So this is more than just style, it's about clarity of intent.
2 of 23
25
In any case it would have ever been helpful for me, there's a better way to do what I was trying to do. In your case you can use os module which I'd argue is a bit more idiomatic. os.path.splitext(file_name)[1].lstrip('.') Or since if I have file paths I ususally like to work with pathlib: Path(file_name).suffix.lstrip('.') Either case makes it very clear what is happening
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Add safe `.get` method to List - Ideas - Discussions on Python.org
August 29, 2023 - To access an item in a dictionary, you use indexing: d["some_key"]. However, if the key doesnโ€™t exist in the dictionary, a KeyError is raised. To avoid this, you can use .get and pass in a default value to return instead: d.get("some_key", "default value"). Lists donโ€™t have such a method ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.6 documentation
To avoid getting this error when trying to access a possibly non-existent key, use the get() method instead, which returns None (or a specified default value) if the key is not in the dictionary. Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in insertion order (if you want it sorted, just use sorted(d) instead).
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists.asp
Python Lists
MongoDB Get Started MongoDB Create DB MongoDB Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB Limit ... Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ use-get-method-to-create-a-dictionary-in-python-from-a-list-of-elements
Use get() method to Create a Dictionary in Python from a List of Elements - GeeksforGeeks
July 15, 2025 - For example, given the list: a = ["apple", "banana", "apple", "orange", "banana", "apple"] then we should create the dictionary: {'apple': 3, 'banana': 2, 'orange': 1} Instead of checking if the key exists we use get() to provide a default count of 0, making the code more concise and efficient. ... a = ["apple", "banana", "apple", "orange", "banana", "apple"] d = {} for item in a: d[item] = d.get(item, 0) + 1 print(d)
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ how-to-get-the-index-of-an-item-in-a-list-in-python
How to Get the Index of an Item in a List in Python - StrataScratch
September 6, 2024 - Master Python's index() function to efficiently locate items in lists, handle errors with custom functions, and improve data analysis using advanced techniques.
Find elsewhere
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
How can I get a value at any position of a list - Python Help - Discussions on Python.org
October 1, 2022 - I have a numbers added in the list, and want to get a value at a particular number from the list. I tried one = (open(input("Open list: ")).read().splitlines()) list = one print(list) for i in range(len(list)): โ€ฆ
๐ŸŒ
Quora
quora.com โ€บ How-do-I-get-a-specific-item-from-a-list-in-Python
How to get a specific item from a list in Python - Quora
Answer (1 of 5): If you know index of item , then directly access it using indexing. For example : l =[1,2,3] if I want to access 3 and index of three is known, then directly can be accessed by indexing.l[2] will give 3. By iterating through the list, lets say item to be searched is 3 , then it...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists_access.asp
Python - Access List Items
MongoDB Get Started MongoDB Create DB MongoDB Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB Limit ... Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_lists.htm
Python - Lists
List is one of the built-in data types in Python. A Python list is a sequence of comma separated items, enclosed in square brackets [ ]. The items in a Python list need not be of the same data type.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-lists
Python Lists - GeeksforGeeks
Python list slicing is fundamental concept that let us easily access specific elements in a list. In this article, weรขย€ย™ll learn the syntax and how to use both positive and negative indexing for slicing with examples.Example: Get the items from a list starting at position 1 and ending at position 4 (e
Published ย  June 3, 2025
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-get-a-list-as-input-from-user
Get a list as input from user in Python - GeeksforGeeks
a = [] n = int(input("Enter the number of elements: ")) for i in range(n): element = input(f"Enter element {i+1}: ") a.append(element) print("List:", a) ... Enter the number of elements: 3 Enter element 1: Python Enter element 2 : is Enter element ...
Published ย  September 18, 2025
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-get-first-and-last-elements-of-a-list
Get first and last elements of a list in Python - GeeksforGeeks
List comprehension can be used to pick specific positions from a list. You can get the first and last elements using [a[i] for i in (0, -1)]. This method works well but is less readable compared to indexing.
Published ย  July 11, 2025
๐ŸŒ
Milliams
milliams.com โ€บ courses โ€บ beginning_python โ€บ Lists.html
Lists - Beginning Python
The code my_list[1] means "give me the number 1 element of the list my_list". Run this code and see what you get. Is it what you expect? You'll probably notice that it prints dog whereas you may have expected it to print cat. This is because in Python you count from zero when indexing lists and so index 1 refers to the second item in the list.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-find-in-list-how-to-find-the-index-of-an-item-or-element-in-a-list
Python Find in List โ€“ How to Find the Index of an Item or Element in a List
February 24, 2022 - The value for end parameter would then be the length of the list minus 1. The index of the last item in a list is always one less than the length of the list. So, putting all that together, here is how you could try to get all three instances of the item: programming_languages = ["JavaScript","Python","Java","Python","C++","Python"] print(programming_languages.index("Python",1,5)) #output #1
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_ref_list.asp
Python List/Array Methods
MongoDB Get Started MongoDB Create DB MongoDB Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB Limit ... Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ access-list-items-in-python
Access List Items in Python - GeeksforGeeks
July 23, 2025 - Python ยท a = [10, 20, 30, 40, 50] # Get a slice of the list from index 1 to 3 print(a[1:4]) Output ยท [20, 30, 40] Sometimes we need to access all the items in the list, and for that, a loop is useful. A for loop lets us go through each item in the list one by one.
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ python-get-index-of-item-list
How to Get the Index of an Item in a List in Python | LearnPython.com
Letโ€™s see an example of a Python list. >>> names = ["Jane", "James", "Matt", "Ashley", "Oliver"] >>> type(names) list ยท One of the operations we perform on lists is to get the index of an item.