1

I'm trying to figure how to pick all the fruits like:

[['id', 'sub type', 'type'], ['1', 'apples', 'fruit'], ['15', 'orange', 'fruit'], ['3', 'corn', 'vegtable']]

How do I output:

['sub type','apple','orange','corn']
George
  • 269
  • 2
  • 5
  • 11

2 Answers2

4

Simple as a list comprehension:

>>> lst = [['id', 'sub type', 'type'], ['1', 'apples', 'fruit'], ['15', 'orange', 'fruit'], ['3', 'corn', 'vegtable']]
>>> [x[1] for x in lst]
['sub type', 'apples', 'orange', 'corn']
>>>
2

operator.itemgetter(1) can be higher-performance

('Trivial answer converted to comment'?! WTF!) This is the one-line reality. Prefer itemgetter to lambda expressions, for performance. Show us more context in which this code occurs, if you can.

Community
  • 1
  • 1
smci
  • 32,567
  • 20
  • 113
  • 146