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?

Djaouad
22.8k4 gold badges37 silver badges57 bronze badges
asked May 12, 2019 at 23:42
1

3 Answers 3

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']
answered May 12, 2019 at 23:49
Sign up to request clarification or add additional context in comments.

Comments

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']
answered May 12, 2019 at 23:46

3 Comments

welc. glad to help!
Hey, hi again! Do you know how add an '\n' to each element?
a[0]+'\n'. this will add a new line
1

You can use

letters,numbers = tuple(zip(*array))
answered May 12, 2019 at 23:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.