Reputation: 303
Using python, I'd like to convert this list of lists:
list_3_5 = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
To a different shape -- a list of 6 lists, where the first 3 new lists have the first three values from the each of the 3 main lists in list_3_5, and then the last 3 new lists have the remaining values from list_3_5:
[[1,2,3],[6,7,8],[11,12,13],[4,5],[9,10],[14,15]]
Any help appreciated.
Updates:
For longer lists, this:
list2 = [list(range(1, 34)), list(range(34, 67)), list(range(67, 100))]
list2b = [list2[i][:5] for i in range(0, len(list2))] + [
list2[i][5:] for i in range(0, len(list2))
]
or this
list2 = [list(range(1, 34)), list(range(34, 67)), list(range(67, 100))]
list2b = list(range(2 * len(list2)))
for key, element in enumerate(list2):
list2b[key] = element[:5]
list2b[key + len(list2)] = element[5:]
Gives this (modifies my original list of 3 lists each with 33 elements, into a list of 6 lists, the first three lists each having 5 elements, and the last three each having 28) :
[[1, 2, 3, 4, 5],
[34, 35, 36, 37, 38],
[67, 68, 69, 70, 71],
[6, 7, 8, ..., 33],
[39, 40, 41, ..., 66],
[72, 73, 74, ... 99]]
But what I would like is this (for this example, a list of 19 lists with 5 elements each, and 1 list at the end with remaining 4 elements):
[[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
....
....
[91, 92, 93, 94, 95],
[96, 97, 98, 99]]
seems like this, but I can't quite see how to modify for my case: reshape an irregular list in python
Upvotes: 0
Views: 1000
Reputation: 909
Here is a solution with double for loops that will work for an arbitrary length list:
list_3_5 = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
newlist = []
for element in list_3_5:
newlist.append(element[:3])
for element in list_3_5:
newlist.append(element[3:])
newlist
Out: [[1, 2, 3], [6, 7, 8], [11, 12, 13], [4, 5], [9, 10], [14, 15]]
But wait! There is more:
list_3_5 = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
newlist = list(range(2*len(list_3_5)))
for key, element in enumerate(list_3_5):
newlist[key] = element[:3]
newlist[key+len(list_3_5)] = element[3:]
newlist
[[1, 2, 3], [6, 7, 8], [11, 12, 13], [4, 5], [9, 10], [14, 15]]
Below part answers the updated section of the question:
biglist = []
for i in list2:
biglist = biglist + i
#Here we put all small lists in the given list into a big list
print(biglist)
#construct an empty list to save the result as we go:
newlist = []
#add sections of 5 to newlist
while len(biglist) >= 5:
newlist.append(biglist[:5])
biglist = biglist[5:]
#add the remaining part of biglist to the newlist:
newlist.append(biglist)
print(newlist)
Output is:
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28, 29, 30], [31, 32, 33, 34, 35], [36, 37, 38, 39, 40], [41, 42, 43, 44, 45], [46, 47, 48, 49, 50], [51, 52, 53, 54, 55], [56, 57, 58, 59, 60], [61, 62, 63, 64, 65], [66, 67, 68, 69, 70], [71, 72, 73, 74, 75], [76, 77, 78, 79, 80], [81, 82, 83, 84, 85], [86, 87, 88, 89, 90], [91, 92, 93, 94, 95], [96, 97, 98, 99]]
Upvotes: 1
Reputation: 56
If you want to have a general code to separate first three elements, try this:
new_list=[list_3_5[i][:3] for i in range(0,len(list_3_5))]+[ list_3_5[i][3:] for i in range(0,len(list_3_5))]
Upvotes: 1
Reputation: 10624
Try this:
[list_3_5[0][:3],list_3_5[1][:3], list_3_5[2][:3], list_3_5[0][3:],list_3_5[1][3:], list_3_5[2][3:]]
Output:
[[1, 2, 3], [6, 7, 8], [11, 12, 13], [4, 5], [9, 10], [14, 15]]
Upvotes: 0