Raises exception if not exactly one item:

Sequence unpacking:

singleitem, = mylist
# Identical in behavior (byte code produced is the same),
# but arguably more readable since a lone trailing comma could be missed:
[singleitem] = mylist
# Not a fan of this one, as the comma still feels missable but the parentheses do
# draw attention (they're completely superfluous), so it's less likely to be missed
# Still 100% identical in behavior, with identical byte code
(singleitem,) = mylist

Rampant insanity, unpack the input to the identity lambda function:

# The only even semi-reasonable way to retrieve a single item and raise an exception on
# failure for too many, not just too few, elements as an expression, rather than a
# statement, without resorting to defining/importing functions elsewhere to do the work
singleitem = (lambda x: x)(*mylist)

All others silently ignore spec violation for too many items, producing first or last item:

Explicit use of iterator protocol:

singleitem = next(iter(mylist))

Destructive pop:

singleitem = mylist.pop()

Negative index:

singleitem = mylist[-1]

Set via single iteration for (because the loop variable remains available with its last value when a loop terminates):

for singleitem in mylist: break

There are many others (combining or varying bits of the above, or otherwise relying on implicit iteration), but you get the idea.

Answer from ShadowRanger on Stack Overflow
Top answer
1 of 3
195

Raises exception if not exactly one item:

Sequence unpacking:

singleitem, = mylist
# Identical in behavior (byte code produced is the same),
# but arguably more readable since a lone trailing comma could be missed:
[singleitem] = mylist
# Not a fan of this one, as the comma still feels missable but the parentheses do
# draw attention (they're completely superfluous), so it's less likely to be missed
# Still 100% identical in behavior, with identical byte code
(singleitem,) = mylist

Rampant insanity, unpack the input to the identity lambda function:

# The only even semi-reasonable way to retrieve a single item and raise an exception on
# failure for too many, not just too few, elements as an expression, rather than a
# statement, without resorting to defining/importing functions elsewhere to do the work
singleitem = (lambda x: x)(*mylist)

All others silently ignore spec violation for too many items, producing first or last item:

Explicit use of iterator protocol:

singleitem = next(iter(mylist))

Destructive pop:

singleitem = mylist.pop()

Negative index:

singleitem = mylist[-1]

Set via single iteration for (because the loop variable remains available with its last value when a loop terminates):

for singleitem in mylist: break

There are many others (combining or varying bits of the above, or otherwise relying on implicit iteration), but you get the idea.

2 of 3
24

I will add that the more_itertools library has a tool that returns one item from an iterable.

from more_itertools import one


iterable = ["foo"]
one(iterable)
# "foo"

In addition, more_itertools.one raises an error if the iterable is empty or has more than one item.

iterable = []
one(iterable)
# ValueError: not enough values to unpack (expected 1, got 0)

iterable = ["foo", "bar"]
one(iterable)
# ValueError: too many values to unpack (expected 1)

more_itertools is a third-party package > pip install more-itertools

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-get-specific-elements-from-list-in-python
How to get specific elements from list in python - GeeksforGeeks
July 23, 2025 - a = [1, 'geeks', 3, 'for', 5] # accessing specific list item with their indices item1 = a[1] item3 = a[2] item5 = a[4] # negative indexing to get last element, same as a[4] item_5 = a[-1] print(item1, item3, item5, item_5)
Discussions

How can I read 1 value from a list in python? - Stack Overflow
It seems you should start reading the python tutorial. ... And there are so much more operations you can perform on lists. ... You can just do list[i] , i being the index of the element to be read. ... Print the list as many times as there are elements in the list...?! :-P 2016-06-28T06:57:48.337Z+00:00 ... These are the three main methods of looping over lists. ... Fetch the i-th element in aList with aList[i-1]. Positions range from 0 to list length minus one... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Pythonic way to get the single element of a 1-sized list - Stack Overflow
What's the most pythonic way to take the single item of a 1-sized list in python? I usually go for item = singlet_list[0] This would fail for an empty list, but I would like a way to make it fail... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python select specific elements from a list - Stack Overflow
Is there a "pythonic" way of getting only certain values from a list, similar to this perl code: my ($one,$four,$ten) = line.split(/,/)[1,4,10] More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to access List elements in Python - Stack Overflow
I have a list: list = [['vegas','London'], ['US','UK']] How to access each element of this list? More on stackoverflow.com
๐ŸŒ stackoverflow.com
People also ask

What is the fastest way to get one element from a Python list?
Indexing with square brackets, like fruits[0], reads a single element in constant time O(1). The first element is index 0, and negative indices count from the end, so fruits[-1] returns the last item. Indexing a position that does not exist raises IndexError, so check len(list) or wrap the access in a try/except when the index comes from user input.
๐ŸŒ
geeksprogramming.com
geeksprogramming.com โ€บ home โ€บ blog โ€บ 7 ways to extract elements from a python list
7 Ways to Extract Elements from a Python List
Does slicing a Python list change the original list?
No. A slice such as fruits[1:4] returns a new list and leaves the original untouched. That makes slicing safe inside loops and useful for copying: fruits[:] produces a shallow copy you can mutate without affecting the source. Slicing never raises IndexError for out-of-range bounds; it clamps to the list length instead.
๐ŸŒ
geeksprogramming.com
geeksprogramming.com โ€บ home โ€บ blog โ€บ 7 ways to extract elements from a python list
7 Ways to Extract Elements from a Python List
How do I extract elements from several lists at the same time?
Pair them with zip(), which walks multiple iterables together and yields one tuple per position: for num, letter in zip(nums, letters). zip stops at the shortest input, so unequal lengths drop the extra tail. To unpack a list of pairs back into separate sequences, use the star operator: zip(*pairs).
๐ŸŒ
geeksprogramming.com
geeksprogramming.com โ€บ home โ€บ blog โ€บ 7 ways to extract elements from a python list
7 Ways to Extract Elements from a Python List
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to get specific elements from a list? โ€“ most pythonic way!
How to Get Specific Elements From a List? - Most Pythonic Way! - Be on the Right Side of Change
July 2, 2020 - The indices start at 0, therefore the third elementโ€™s index is 2. In Python it is also possible to count from back to front using negative indices. This is very handy, especially for retrieving the last or second last element.
๐ŸŒ
Milliams
milliams.com โ€บ courses โ€บ beginning_python โ€บ Lists.html
Lists - Beginning Python
Indexing a list in Python is done ... above for making a list. To get a single element out of a list you write the name of the variable followed by a pair of square brackets with a single number between them:...
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ list โ€บ extract-elements-python-list
5 Easy Ways To Extract Elements From A Python List - AskPython
December 29, 2021 - The print statement prints the โ€˜1โ€™ element in the index. firstgrid=["A","B","C","D","E","F"] print(firstgrid[1]) ... Here, we created a variable named โ€˜varaโ€™ and we filled the elements into the list.
๐ŸŒ
GeeksProgramming
geeksprogramming.com โ€บ home โ€บ blog โ€บ 7 ways to extract elements from a python list
7 Ways to Extract Elements from a Python List
March 14, 2023 - Pull single items, slices, and filtered subsets from a Python list using indexing, slicing, comprehensions, filter, map, enumerate, and zip.
Find elsewhere
๐ŸŒ
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...
๐ŸŒ
Code.mu
code.mu โ€บ en โ€บ python โ€บ book โ€บ prime โ€บ lists โ€บ element
Getting a Single Element of a List in Python
I'm the author of code.mu :) I'd love to chat with my non-Russian audience. I'm looking for your feedback on the site and the translation quality. Let's chat:) ... You can access the elements of a list in the same way as you access the characters of a string: the first element has the number ...
๐ŸŒ
Real Python
realpython.com โ€บ videos โ€บ retrieving-elements-from-list
Retrieving Elements From a List (Video) โ€“ Real Python
00:12 Individual elements can be retrieved by their zero-based index using the syntax, my_list with the integer index value in square brackets. Alternatively, you can retrieve multiple elements at once using a Python slice.
Published: June 17, 2025
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-select-from-a-list
How to Select Items From a List in Python
January 27, 2026 - For this, Python has a great built-in module called random. import random # List of US Currency denominations bills = [1, 2, 5, 10, 20, 50, 100] # Selecting one random bill random_bill = random.choice(bills) # Selecting three unique random bills random_set = random.sample(bills, 3) print("Single Random Bill:", random_bill) print("Three Random Bills:", random_set)
๐ŸŒ
Okpedia
how.okpedia.org โ€บ en โ€บ python โ€บ how-to-extract-elements-from-python-list
How to extract elements from Python list - Okpedia How
To extract an element from the python list, enter the index of the element in square brackets. ... The argument x is the positive integer indicating the position (index) of an element in the index. The index of the first element of the list is zero, the index of the second element is one, etc.
Top answer
1 of 2
16

This blog post suggests an elegant solution I fell in love with:

(item,) = singlet_list

I find it much more readable, and plus it works with any iterable, even if it is not indexable.

EDIT: Let me dig a little more

This construct is called sequence unpacking or multiple assignment throughout the python documentation, but here I'm using a 1-sized tuple on the left of the assignment.

This has actually a behaviour that is similar to the 2-lines in the initial question: if the list/iterable singlet_list is of length 1, it will assign its only element to item. Otherways, it will fail with an appropriate ValueError (rather than an AssertionError as in the question's 2-liner):

>>> (item,) = [1]
>>> item
1
>>> (item,) = [1, 2]
ValueError: too many values to unpack
>>> (item,) = []
ValueError: need more than 0 values to unpack

As pointed out in the blog post, this has some advantages:

  • This will not to fail silently, as required by the original question.
  • It is much more readable than the [0] indexing, and it doesn't pass unobserved even in complex statements.
  • It works for any iterable object.
  • (Of course) it uses only one line, with respect to explicitly using assert
2 of 2
3

You could use an inline if...else and define a default value like this:

If singlet_list contains one or more values:

singlet_list = [2]
item = singlet_list[0] if singlet_list else False
print item

output:

2

If singlet_list is empty:

singlet_list = []
item = singlet_list[0] if singlet_list else False
print item

output:

False

This uses the fact that an empty list evaluates to False.


Similarly, if you would like a default value to be assigned if the list doesn't contain exactly one element, you could check the length:

item = singlet_list[0] if len(singlet_list) == 1 else False
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ list โ€บ get-first-last-elements-python-list
How to Get the First and Last Elements of a Python List? - AskPython
September 24, 2020 - You can access the first element from the list by using the name of the list along with index 0. ... Use the below syntax to print the first element of a list. ... You can access the last element by using Negative Index.
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ python-get-first-element-list
How to get the First Element of a List in Python - Sabe.io
March 25, 2022 - Simply put the index of the element you want to access. Because we want to get the first element of the list, we can use the index 0. ... We use 0 instead of 1 because Python is zero-based meaning that the first element is at index 0 and not 1.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ extract-elements-from-a-python-list
Extract Elements from a Python List - GeeksforGeeks
July 23, 2025 - When working with lists in Python, we often need to extract specific elements. The easiest way to extract an element from a list is by using its index.
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ python โ€บ first, last, initial, head, tail
First, last, initial, head, tail of a Python list - 30 seconds of code
May 15, 2024 - To get the first element of a list (also known as the head of the list), you can use lst[0]. This will return the first element of the list, or None if the list is empty.
๐ŸŒ
SitePoint
sitepoint.com โ€บ python hub โ€บ access list items
Python - Access List Items | SitePoint โ€” SitePoint
This is handy when you need to quickly access an element from the end without knowing the listโ€™s length. If you try to access an index that doesnโ€™t exist in the list, Python raises an IndexError.