Python | Split list in uneven groups
Last Updated :
09 May, 2023
Sometimes, while working with python, we can have a problem of splitting a list. This problem is quite common and has many variations. Having solutions to popular variations proves to be good in long run. Let’s discuss certain way to split list in uneven groups as defined by other list.
Method 1: Using itertools.islice() + list comprehension The combination of above functionalities can be used to perform this task. In this, islice() is used to perform the core task of slicing the list and list comprehension is used to perform the task of binding together logic and iterations. The container is converted to iterator for faster iteration.
Python3
from itertools import islice
test_list = [ 1 , 4 , 5 , 7 , 6 , 5 , 4 , 2 , 10 ]
split_list = [ 3 , 4 , 2 ]
print ("The original list is : " + str (test_list))
print ("The split list is : " + str (split_list))
temp = iter (test_list)
res = [ list (islice(temp, 0 , ele)) for ele in split_list]
print ("The resultant split list is : " + str (res))
|
Output :
The original list is : [1, 4, 5, 7, 6, 5, 4, 2, 10]
The split list is : [3, 4, 2]
The resultant split list is : [[1, 4, 5], [7, 6, 5, 4], [2, 10]]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2: Using math
Take slice of original list from starting to ending and then append it to resultant split and then update
Step-by-Step algorithm
- we can start by computing the number of elements we want in each group.
- We can do this by dividing the total number of elements by the number of groups we want, and rounding up the result using the ceil function from the math module.
- This ensures that all groups except the last one have at least ceil(n/m) elements, where n is the total number of elements and m is the number of groups.
- We can then use a loop to iterate over the input list and split it into groups of the desired size.
- We can use the slice notation to extract a sublist of the input list starting at the current index and ending at the current index plus the desired group size. We can then append this sublist to the output list of groups, and increment the current index by the group size.
Python3
original_list = [ 1 , 4 , 5 , 7 , 6 , 5 , 4 , 2 , 10 ]
split_list = [ 3 , 4 , 2 ]
resultant_split_list = []
start_index = 0
for length in split_list:
slice_list = original_list[start_index:start_index + length]
resultant_split_list.append(slice_list)
start_index + = length
print (resultant_split_list)
|
Output
[[1, 4, 5], [7, 6, 5, 4], [2, 10]]
Time complexity: O(n), where n is the length of the original list. This is because we are iterating over the original list once to create the slices, and the time taken to slice the list is proportional to its length.
Auxiliary Space: O(m), where m is the length of the split list. This is because we are creating a new list for each slice in the resultant split list, and the space taken by each slice is proportional to the length of the corresponding element in the split list.
Method #3: Using the Itertools Module:
Algorithm:
- Import the itertools module.
- Define the original list and split list.
- Use the accumulate function from itertools to calculate the cumulative sum of the split list.
- Use a list comprehension to iterate over the pairs of cumulative sums and calculate the slice of the original list using islice.
- Convert each slice to a list and append it to a new list to obtain the resultant split list.
- Print the resultant split list.
Python3
from itertools import accumulate, islice
original_list = [ 1 , 4 , 5 , 7 , 6 , 5 , 4 , 2 , 10 ]
print ( "The original list is : " + str (original_list))
split_list = [ 3 , 4 , 2 ]
print ( "The split list is : " + str (split_list))
res = [ list (islice(original_list, start, end)) for start, end in zip ([ 0 ] + list (accumulate(split_list)), accumulate(split_list))]
print ( "The resultant split list is : " + str (res))
|
Output
The original list is : [1, 4, 5, 7, 6, 5, 4, 2, 10]
The split list is : [3, 4, 2]
The resultant split list is : [[1, 4, 5], [7, 6, 5, 4], [2, 10]]
Time Complexity: O(n), where n is the total number of elements in the original list.
Auxiliary Space: O(m * k), where m is the length of the split list and k is the length of the longest slice.
Method #4: Use a while loop and slice
Python3
original_list = [ 1 , 4 , 5 , 7 , 6 , 5 , 4 , 2 , 10 ]
split_list = [ 3 , 4 , 2 ]
resultant_split_list = []
pos = 0
for length in split_list:
sublist = original_list[pos:pos + length]
resultant_split_list.append(sublist)
pos + = length
if pos < len (original_list):
resultant_split_list[ - 1 ].extend(original_list[pos:])
print (resultant_split_list)
|
Output
[[1, 4, 5], [7, 6, 5, 4], [2, 10]]
Time complexity of this approach is O(n^2) because the list comprehension inside the while loop iterates over all elements in the slice, which grows with each iteration.
The auxiliary space complexity is O(n), because we are creating a new slice list for each iteration.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...