Kosh
Kosh

Reputation: 1246

Get a column from the list of tuples

I have a list of tuples. Is it possible to get a column from each tuple without using numpy, pandas etc?

lst = [('a','b','c'),
       ('d','e','f')]

Lets say I want to get 'b','e', but with

lst[:][1]

I get

('d', 'e', 'f')

Upvotes: 3

Views: 7493

Answers (2)

Nicolas Gervais
Nicolas Gervais

Reputation: 36614

You can use map():

list(map(lambda x: x[1], lst))
['b', 'e']

For all items in your list (i.e., every tuple), it will select the element at index 1.

Upvotes: 6

AnthonyHein
AnthonyHein

Reputation: 131

What you are looking for is called "list comprehension", here is a taste of it that addresses your question:

lst = [('a','b','c'),
       ('d','e','f')]

col1 = [tple[1] for tple in lst]
print(col1)

Upvotes: 4

Related Questions