0

I have the following list:

a = [[1,2],[3,4],[5,6]]

Now I want to access only the first elements of each sub-list:

a1 = [1,3,5]

How can I do it ?

  • 1
    `a1 = [i[0] for i in a]` – Miraj50 Nov 02 '17 at 09:19
  • wow, that was fast! Thanks a lot ! :) –  Nov 02 '17 at 09:20
  • I'm sure this is a duplicate but you can either use [list comprehension](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions) `[x[0] for x in a]` or [zip](https://docs.python.org/3.3/library/functions.html#zip) function `list(zip(*a))[0]`. – umutto Nov 02 '17 at 09:20
  • 2
    that _is_ a duplicate, found it by googling in seconds. This kind of duplicate Q&mostly answering is kiling the site. – Jean-François Fabre Nov 02 '17 at 09:21

1 Answers1

1

enter image description here

Try this:

a = [[1,2],[3,4],[5,6]]
b = [i[0] for i in a]
print(b)  # prints [1,3,5]
Ma0
  • 15,057
  • 4
  • 35
  • 65
karthik reddy
  • 479
  • 4
  • 12