Categories
Python Answers

How to extract the n-th elements from a list of tuples with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *