0

i have a python table like this

t = [
  ['a', 1, True],
  ['b', 2, True],
  ['c', 3, True],
  ['d', 4, True]
]

i want to extract the 1st element list like this

l = ['a', 'b', 'c', 'd']

i know how tod do it with a for loop, but i can't do it with one line of code, i've tried this

t[:][0]

and also this :

t[0][:]

but this does not give me what i need

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48

3 Answers3

0

One option (and probably the fastest) is a list comprehension.

[ s[0] for s in t ]
LaytonGB
  • 1,384
  • 1
  • 6
  • 20
0

You can try this:

list(zip(*t))[0]

0

You can use zip and unpack the list in it:

zip(*t)

Then convert that to a list:

new1=list(zip(*t))

This returns

[('a', 'b', 'c', 'd'), (1, 2, 3, 4), (True, True, True, True)]

Now you can simply fetch the first element of the list:

print(new1[0])