Python Forum
Take first Elements of sublists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Take first Elements of sublists (/thread-33449.html)



Take first Elements of sublists - quest - Apr-26-2021

This is my list:

list_of_lists = [[1, 2], [3, 4]]
I want to group of elements of sublists:

list_of_lists = [[1, 2], [3, 4]]
n=[]
for list in list_of_lists:
   n.append(list[0]) #I am taking 1 and 3
   n.append(list[1]) # I am taking 2 and 4 
But sometimes I can have 100 elements too so I want to do it dynamically instead of writing [0],[1]

How can I do that?


RE: Take first Elements of sublists - perfringo - Apr-26-2021

Have a look at built-in zip


RE: Take first Elements of sublists - buran - Apr-26-2021

list_of_lists = [[1, 2], [3, 4]]
print(list(zip(*list_of_lists)))
Output:
[(1, 3), (2, 4)]