Reputation: 5
I wrote a code that takes a certain input (int) and stores it to a list.
The next phase, which I can't manage to construct is to take the list and split it to 4 equal lists.
So far I take the sum of the prime_list
and divide it to 4. the sum stored as var to further use.
prime_list = [1,2,3,4,5,6,7,8,9,10,11,12]
split to equal 4 lists, divide by range (or other methods):
split_list_01 = [1,2,3]
split_list_02 = [4,5,6]
split_list_03 = [7,8,9]
split_list_04 = [10,11,12]
Upvotes: 0
Views: 35
Reputation: 24905
Try this:
prime_list = [1,2,3,4,5,6,7,8,9,10,11,12]
new_list = [prime_list[i:i+3] for i in range(0, len(prime_list), 3)]
print new_list
Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
A dictionary can be used to store each of the lists using different keys which can be accessed later:
prime_list = [1,2,3,4,5,6,7,8,9,10,11,12]
split_list = {}
count = 0
for i in range(0, len(prime_list), 3):
split_list[count] = prime_list[i:i+3]
count += 1
print split_list
Output:
{0: [1, 2, 3], 1: [4, 5, 6], 2: [7, 8, 9], 3: [10, 11, 12]}
Upvotes: 1