DEV Community

Muthu
Muthu

Posted on

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)