1

I have this array that contains some other arrays in Python, but I need only the first elements of each mini array inside the main array. Is there some method to do that?

Example:

array = [['a','1'], ['b','2'], ['c','3'], ['d','4'], ['e','5']]

I need the letters in one row:

'a'
'b'
'c'
'd'
'e'

And the numbers in another:

'1'
'2'
'3'
'4'
'5'

can You help me with this?

DjaouadNM
  • 22,013
  • 4
  • 33
  • 55

3 Answers3

2

You can use zip to separate letters from numbers and map to convert the tuples returned by zip to lists:

array = [['a','1'], ['b','2'], ['c','3'], ['d','4'], ['e','5']]

letters, numbers = map(list, zip(*array))

print(letters)
print(numbers)

Output:

['a', 'b', 'c', 'd', 'e']
['1', '2', '3', '4', '5']
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
1

You can use comprehension. a[0] means first item in a list

[a[0] for a in array]

Result:
['a', 'b', 'c', 'd', 'e']
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
1

You can use

letters,numbers = tuple(zip(*array))