Open In App

How to Split the list by some value ?

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with lists in Python, we might need to split them into smaller sublists based on a specific value, often called a delimiter. This operation can be useful in tokenizing strings or working with structured datasets. Below are several methods to split a list by a specific value starting from the most efficient.

Using itertools.groupby

itertools.groupby groups adjacent elements together based on the condition. We can then filter out groups where the delimiter is present, retaining only the non-delimited groups.

Python
from itertools import groupby

def split_by_value_groupby(lst, delimiter):
  # Using groupby to group consecutive elements 
  #based on whether they are equal to the delimiter
    return [list(group) for k, group in groupby(lst, lambda x: x == delimiter) if not k]

print(split_by_value_groupby([1, 2, 0, 3, 4, 0, 5], 0))

Output
[[1, 2], [3, 4], [5]]
  • groupby groups elements based on whether they match the delimiter.
  • We only retain groups where the key (matching the delimiter) is False.

Let's explore some more methods and see how we can split the list by some value

Using a Loop with Temporary List

We iterate through the list and use a temporary sublist to collect elements until we encounter the delimiter. Once the delimiter is found, the sublist is appended to the result and the temporary list is cleared.

Python
def split_by_value(a, delimiter):
   # Initialize two lists: 
    # result will store the final split sublists
    result, temp = [], []
    
    # Iterate over each item in the input list 'a'
    for item in a:
        if item == delimiter:
          
          # If the item is the delimiter, append the accumulated temp list to result
            result.append(temp)
            temp = []
        else:
            temp.append(item)
    result.append(temp)
    return result

print(split_by_value([1, 2, 0, 3, 4, 0, 5], 0))

Output
[[1, 2], [3, 4], [5]]
  • A temp list accumulates elements.
  • When the delimiter is encountered, the temp list is added to the result, and it is reset for the next group.

Using Index and Slicing

In this method, we manually find the indices of the delimiter and slice the list accordingly.

Python
def split_by_value_slicing(a, delimiter):
    result, start = [], 0
    
    for i, item in enumerate(a):
      # Check if the current item is the delimiter
        if item == delimiter:
            result.append(a[start:i])
            start = i + 1
            
    result.append(a[start:])
    # Update 'start' to be the next index after the delimiter
    return result

print(split_by_value_slicing([1, 2, 0, 3, 4, 0, 5], 0))

Output
[[1, 2], [3, 4], [5]]

Explanation:

  • Track the start index of each group.
  • Slice the list from start to the current index when the delimiter is found.

Using Recursion

A recursive approach can also split the list based on the delimiter, although it’s less efficient and not suitable for very large lists.

Python
def split_by_value_recursive(a, delimiter):
    if delimiter not in a:
        return [a]
      
       # Find the index of the first occurrence of the delimiter
    idx = a.index(delimiter)
    return [a[:idx]] + split_by_value_recursive(a[idx + 1:], delimiter)

print(split_by_value_recursive([1, 2, 0, 3, 4, 0, 5], 0))

Output
[[1, 2], [3, 4], [5]]

Explanation:

  • If the delimiter is not in the list, return the list as a single group.
  • Spliting the list at the first occurrence of the delimiter and calling the function on the remaining list.

Practice Tags :

Similar Reads