2

What is the most Pythonic way to print the first element of each of the lists?

For example, I want ['apple', 'banana'] from the list of lists below:

data = [['apple','airplane'],['banana','boat']]

This is my best attempt:

fruit = [list(fruit) for fruit in data]

[letter[0] for letter in fruit]

However, having two list comprehensions doesn't seems very pythonic

Anton
  • 4,765
  • 12
  • 36
  • 50

1 Answers1

0

Why not just

fruit = [fruit[0] for fruit in data]

That gets me the expected output, see below:

>>> data = [['apple','airplane'],['banana','boat']]
>>> fruit = [fruit[0] for fruit in data]
>>> print fruit
['apple', 'banana']
user3543300
  • 499
  • 2
  • 9
  • 27