Sometimes, we want to extract the n-th elements from a list of tuples with Python.
In this article, we’ll look at how to extract the n-th elements from a list of tuples with Python.
How to extract the n-th elements from a list of tuples with Python?
To extract the n-th elements from a list of tuples with Python, we can use list comprehension.
For instance, we write
elements = [(1,1,1),(2,3,7),(3,5,10)]
n = 1
l = [x[n] for x in elements]
to get the 2nd element from each tuple x
in the elements
list and assign the list to l
.
Conclusion
To extract the n-th elements from a list of tuples with Python, we can use list comprehension.