Python Forum

Full Version: Take first Elements of sublists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
Have a look at built-in zip
list_of_lists = [[1, 2], [3, 4]]
print(list(zip(*list_of_lists)))
Output:
[(1, 3), (2, 4)]