DEV Community

Muthu
Muthu

Posted on

1 1

Add all the first elements and second elements to new list from a list of list using `list comprehension`

Question:
Add all the first elements and second elements to new list from a list of list using list comprehension

Ex:
Given: [ [1,2], [3,4], [5,6] ]
Expected: [[1, 3, 5], [2, 4, 6]]

Solution:

a = [ [1,2], [3,4], [5,6] ]
final_list = [[x[0] for x in a], [x[1] for x in a]]
print(final_list)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay