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']
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']
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']
>>>
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.