-1

I am using a list of list as follows:

L = [['a', 4], ['b', 2], ['c', 13]]

I want to generate a new list for all the numbers which are in the second position in each set of the list:

L =  [4, 2, 13]

Is there any shortcut in python to get the above list?

TerryA
  • 58,805
  • 11
  • 114
  • 143
Pradeep
  • 6,303
  • 9
  • 36
  • 60
  • yeah, two possible ways to do the same thing, i was unaware of zip, so couldn't find it earlier – Pradeep Sep 11 '13 at 10:28
  • 2
    @pradeep [Here's a link to the documentation on `zip()`](http://docs.python.org/2/library/functions.html#zip) – TerryA Sep 11 '13 at 10:30

1 Answers1

6

Use a list comprehension:

>>> L = [['a', 4], ['b', 2], ['c', 13]]
>>> print [i[1] for i in L]
[4, 2, 13]

This accesses the second item in each list (remember that indexing starts at zero, so 1 gets the second index)

TerryA
  • 58,805
  • 11
  • 114
  • 143