Python | Custom list split
Last Updated :
06 Apr, 2023
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
OutputThe 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)
OutputThe 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:
- Initialize the list test_list and the split index list split_list.
- Print the original list and the original split index list.
- Split the list using the slice function and append the resulting sublists to a new list res.
- 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.
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read