0

i need help working on a list: i have a list like this

[[1, 2, 3, 5], [3, 4, 5, 6], [7, 8, 9, 3],[10, 11, 12, 13]]

how do I get the first element of all the list within the list.

desired output:

1
3
7
10
radrow
  • 6,419
  • 4
  • 26
  • 53

1 Answers1

1

You just iterate as it were a flat list

lists = [[1, 2, 3, 5], [3, 4, 5, 6], [7, 8, 9, 3],[10, 11, 12, 13]]
 
for l in lists:
   if l != []:
     print(l[0])
radrow
  • 6,419
  • 4
  • 26
  • 53
  • thank you, do you have an idea, how i can put the out put in a list or array like ['1', '3', '7', '10'] or {'1' , '3', '7', '10'} – Noble somto Jan 27 '22 at 08:13
  • 1
    @Noblesomto initialize a list with ```firsts = []``` before the for loop and replace the print statement in the code with ```firsts.append(l[0])``` – felix Jan 27 '22 at 08:20