Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Custom list split

Python | Custom list split

Development and sometimes machine learning applications require splitting lists into smaller list in a custom way, i.e on certain values on which split has to be performed. This is quite a useful utility to have knowledge about. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using list comprehension + zip() By coupling the power of list comprehension and zip(), this task can be achieved. In this, we zip beginning and end of list and then keep slicing the list as they arrive and cutting off new lists from them. 

Python3




# Python3 code to demonstrate
# to perform custom list split
# using list comprehension + zip()
 
# initializing string 
test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
 
# initializing split index list
split_list = [2, 5, 7]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing original split index list
print ("The original split index list : " + str(split_list))
 
# using list comprehension + zip()
# to perform custom list split
res = [test_list[i : j] for i, j in zip([0] +
          split_list, split_list + [None])]
 
# printing result
print ("The splitted lists are : " +  str(res))


Output:

The original list is : [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
The original split index list : [2, 5, 7]
The splitted lists are : [[1, 4], [5, 6, 7], [3, 5], [9, 2, 4]]

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(m), where m is the length of the split index list. 

Method #2 : Using itertools.chain() + zip() The task performed by the list comprehension function of getting the split chunks can also be done using chain function. This is more useful when we wish to handle larger lists as this method is more efficient. 

Python3




# Python3 code to demonstrate
# to perform custom list split
# using itertools.chain() + zip()
from itertools import chain
 
# initializing string 
test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
 
# initializing split index list
split_list = [2, 5, 7]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing original split index list
print ("The original split index list : " + str(split_list))
 
# using itertools.chain() + zip()
# to perform custom list split
temp = zip(chain([0], split_list), chain(split_list, [None]))
res = list(test_list[i : j] for i, j in temp)
 
# printing result
print ("The splitted lists are : " +  str(res))


Output:

The original list is : [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
The original split index list : [2, 5, 7]
The splitted lists are : [[1, 4], [5, 6, 7], [3, 5], [9, 2, 4]]

Time complexity: O(n), where n is the length of the test_list, because we need to iterate over the entire list once.
Auxiliary space: O(k), where k is the number of split indices in the split_list, because we need to create a new list of length k+1 to store the split indices. 

Method #3 : Using slice() + iteration

Using the slice function to split the list into smaller lists. The slice function can be used to create a slice object representing a range of indices, which can then be passed as an argument to the list function to create a new list from the original list.

Python3




# initializing list
test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
 
# initializing split index list
split_list = [2, 5, 7]
 
# printing original list
print("The original list is:", test_list)
 
# printing original split index list
print("The original split index list:", split_list)
 
# using the slice function to split the list
res = []
start = 0
for end in split_list:
    res.append(test_list[slice(start, end)])
    start = end
res.append(test_list[slice(start, len(test_list))])
 
# printing result
print("The splitted lists are:", res)
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is: [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
The original split index list: [2, 5, 7]
The splitted lists are: [[1, 4], [5, 6, 7], [3, 5], [9, 2, 4]]

Time complexity: O(n), where n is the length of the list. This is because the slice function has a time complexity of O(1) and the loop iterates over all elements in the list, resulting in a total time complexity of O(n).
Auxiliary space: O(n), since a new list is created for each split and the length of the new list is equal to the number of elements in the original list. This results in a total space complexity of O(n).

Method #4: Using a loop with range() and append()

Loop is used to iterate over the elements between each pair of split indices and append them to a temporary list. This temporary list is then added to the final result list.

Python3




# initializing list
test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
 
# initializing split index list
split_list = [2, 5, 7]
 
# printing original list
print("The original list is:", test_list)
 
# printing original split index list
print("The original split index list:", split_list)
 
# using a loop to split the list
res = []
start = 0
for end in split_list:
    temp = []
    for i in range(start, end):
        temp.append(test_list[i])
    res.append(temp)
    start = end
temp = []
for i in range(start, len(test_list)):
    temp.append(test_list[i])
res.append(temp)
 
# printing result
print("The splitted lists are:", res)


Output

The original list is: [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
The original split index list: [2, 5, 7]
The splitted lists are: [[1, 4], [5, 6, 7], [3, 5], [9, 2, 4]]

Time complexity: O(n), where n is the length of the input list test_list. 
Auxiliary space: O(n), where n is the length of the input list test_list.

Method #5: Using the built-in function itertools.islice()

The itertools.islice() function returns an iterator that slices elements from a given iterable (in this case, the test_list) between a start and end index. We can use this function to split the list based on the values in the split_list.

Step-by-step approach:

  • Import the itertools module.
  • Initialize an empty list to hold the split lists.
  • Initialize a start index variable to 0.
  • Use the zip() function to iterate over pairs of consecutive values in the split_list.
  • For each pair of values (start, end) in the split_list, use itertools.islice() to slice the test_list between the start and end indices and append the result to the split list.
  • If there are any remaining elements in the test_list after the last split index, use itertools.islice() to slice those elements and append the result to the split list.
  • Return the split list.

Below is the implementation of the above approach:

Python3




import itertools
 
def split_list(test_list, split_points):
    splits = []
    start = 0
    for end in itertools.chain(split_points, [None]):
        split = list(itertools.islice(test_list, start, end))
        splits.append(split)
        start = end
    return splits
 
# Example usage:
test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
split_points = [2, 5, 7]
result = split_list(test_list, split_points)
print(result)


Output

[[1, 4], [5, 6, 7], [3, 5], [9, 2, 4]]

Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(m), where m is the number of splits (i.e. the length of the split_list plus 1.

Method #6: Using numpy:

Algorithm:

  1. Initialize the list test_list and the split index list split_list.
  2. Print the original list and the original split index list.
  3. Split the list using the slice function and append the resulting sublists to a new list res.
  4. Print the resulting sublists res.

Python3




import numpy as np
 
# initializing list
test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
 
# initializing split index list
split_list = [2, 5, 7]
 
# printing original list
print("The original list is:", test_list)
 
# printing original split index list
print("The original split index list:", split_list)
 
# using NumPy to split the list
res = np.split(test_list, split_list)
res = [sublist.tolist() for sublist in res]
 
# printing result
print("The splitted lists are:", res)
#This code is contributed by Jyothi pinjala.


Output:
The original list is: [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
The original split index list: [2, 5, 7]
The splitted lists are: [[1, 4], [5, 6, 7], [3, 5], [9, 2, 4]]

Time Complexity:
The time complexity of initializing the two lists is O(n), where n is the length of the test_list.
The time complexity of printing the two lists is also O(n).
The time complexity of the for loop that splits the list is O(k), where k is the length of the split_list.
The time complexity of the slice function is O(1) since it is a constant time operation.
Therefore, the total time complexity of the code is O(n+k).

Auxiliary Space:
The space complexity of the code is O(n) because we are creating a new list res to store the resulting sublists, and this list can have up to n elements (if split_list is empty).
Note that the space complexity of the original list and the split index list is not counted because they are part of the input.

RELATED ARTICLES

Most Popular

Recent Comments