So I am new to python and am having difficulties on writing an efficient for loop on a list of lists. I have a list called "mylist" and I need to store the values of the first column of sublists, and for that I have written the following:
The problem is the the answer I get is a list with "len = 50"; meanwhile I need to keep the shape of the "newlist" similar to "mylist" i.e. I want the final answer to be a list of 5 lists, and then in each list there should be 10 other lists. (Please see the attached files; I need to get something like "wanted_list"). I tried different things like:
or:
But obviously non of these work. I was wondering if you could teach me how I can do this!
1 2 3 4 5 |
newlist = [] for i in range ( 5 ): for j in range ( 10 ): d = [x for x in mylist[i][j][:, 0 ]] newlist.append(d) |
1 2 3 4 5 |
newlist = [] for i in range ( 5 ): for j in range ( 10 ): d[i] = [x for x in mylist[i][j][:, 0 ]] newlist.append(d) |
1 2 3 4 5 |
newlist = [] for i in range ( 5 ): for j in range ( 10 ): d = [x for x in mylist[i][j][:, 0 ]] newlist.append(d) |