74

I have a list like this:

a = [[4.0, 4, 4.0], [3.0, 3, 3.6], [3.5, 6, 4.8]]

I want an outcome like this (EVERY first element in the list):

4.0, 3.0, 3.5

I tried a[::1][0], but it doesn't work

Georgy
  • 12,464
  • 7
  • 65
  • 73
CodingBeginner
  • 768
  • 1
  • 5
  • 6
  • Does this answer your question? [Extract first item of each sublist](https://stackoverflow.com/questions/25050311/extract-first-item-of-each-sublist) – Georgy Dec 11 '20 at 15:03

7 Answers7

119

You can get the index [0] from each element in a list comprehension

>>> [i[0] for i in a]
[4.0, 3.0, 3.5]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
66

Use zip:

columns = zip(*rows) #transpose rows to columns
print columns[0] #print the first column
#you can also do more with the columns
print columns[1] # or print the second column
columns.append([7,7,7]) #add a new column to the end
backToRows = zip(*columns) # now we are back to rows with a new column
print backToRows

You can also use numpy:

a = numpy.array(a)
print a[:,0]

Edit: zip object is not subscriptable. It need to be converted to list to access as list:

column = list(zip(*row))
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 5
    in python 3, you need to do list(zip(*rows)), otherwise it is not subscriptable. – mehmet May 02 '17 at 01:00
  • Thanks for this nice solution. Can you please explain a little bit how come zip(*rows) transpose the 2D list? – notilas Jul 31 '19 at 03:49
10

You could use this:

a = ((4.0, 4, 4.0), (3.0, 3, 3.6), (3.5, 6, 4.8))
a = np.array(a)
a[:,0]
returns >>> array([4. , 3. , 3.5])
Angel Lira
  • 293
  • 3
  • 12
  • This should be voted the best answer. Numpy slicing will out perform loops or list comprehensions any day – Jace999 Aug 30 '23 at 09:12
7

You can get it like

[ x[0] for x in a]

which will return a list of the first element of each list in a

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
7

Compared the 3 methods

  1. 2D list: 5.323603868484497 seconds
  2. Numpy library : 0.3201274871826172 seconds
  3. Zip (Thanks to Joran Beasley) : 0.12395167350769043 seconds
D2_list=[list(range(100))]*100
t1=time.time()
for i in range(10**5):
    for j in range(10):
        b=[k[j] for k in D2_list]
D2_list_time=time.time()-t1

array=np.array(D2_list)
t1=time.time()        
for i in range(10**5):
    for j in range(10):
        b=array[:,j]        
Numpy_time=time.time()-t1

D2_trans = list(zip(*D2_list)) 
t1=time.time()        
for i in range(10**5):
    for j in range(10):
        b=D2_trans[j]
Zip_time=time.time()-t1

print ('2D List:',D2_list_time)
print ('Numpy:',Numpy_time)
print ('Zip:',Zip_time)

The Zip method works best. It was quite useful when I had to do some column wise processes for mapreduce jobs in the cluster servers where numpy was not installed.

notilas
  • 2,323
  • 4
  • 23
  • 36
1

If you have access to numpy,

import numpy as np
a_transposed = a.T
# Get first row
print(a_transposed[0])

The benefit of this method is that if you want the "second" element in a 2d list, all you have to do now is a_transposed[1]. The a_transposed object is already computed, so you do not need to recalculate.

Description

Finding the first element in a 2-D list can be rephrased as find the first column in the 2d list. Because your data structure is a list of rows, an easy way of sampling the value at the first index in every row is just by transposing the matrix and sampling the first list.

Community
  • 1
  • 1
BeardedDork
  • 162
  • 2
  • 13
-1

Try using

for i in a :
  print(i[0])

i represents individual row in a.So,i[0] represnts the 1st element of each row.