Split list into lists by value - Python
Last Updated :
11 Jul, 2025
The goal here is to split a list into sublists based on a specific value in Python. For example, given a list [1, 4, 5, 6, 4, 7, 4, 8] and a chosen value 4, we want to split the list into segments that do not contain the value 4, such as [[1], [5, 6], [7], [8]]. Let’s explore different approaches to split the list by a specified value.
groupby function from itertools groups consecutive elements of a list based on a specific condition. This method loops through the grouped elements and processes only the parts of the list that don't match the condition, effectively splitting the list based on where the condition changes.
Python
from itertools import groupby
a = [1, 4, 5, 6, 4, 5, 6, 5, 4]
b = 4
res = []
for k, g in groupby(a, lambda x: x == b):
if not k:
res.append(list(g))
print(res)
Output[[1], [5, 6], [5, 6, 5]]
Explanation: lambda x: x == b creates groups of True (when equal to b) or False (when not equal). It then checks for False groups and appends them to the result list, effectively splitting the list at occurrences of b and keeping only the segments without b.
Using for loop with manual split
This method involves manually looping through the list and splitting it into sublists whenever a specific element is encountered. It tracks the positions where the list should be split and collects the segments accordingly.
Python
a = [1, 4, 5, 6, 4, 5, 6, 5, 4]
b = 4
res = []
start = 0
for i, v in enumerate(a):
if v == b:
res.append(a[start:i])
start = i + 1
if start < len(a):
res.append(a[start:])
print(res)
Output[[1], [5, 6], [5, 6, 5]]
Explanation: Loops through the list a, splitting it into sublists at occurrences of b and appending segments without b to the result. It tracks the start of each segment and adds any remaining elements after the last occurrence of b.
Using filter
Here, the filter function is used to remove elements that match a certain condition from parts of the list. The list is split into segments and filter is applied to each segment to eliminate unwanted elements, leaving only the desired parts of the list.
Python
a = [1, 4, 5, 6, 4, 5, 6, 5, 4]
b = 4
res = []
start = 0
for i, v in enumerate(a):
if v == b:
res.append(list(filter(lambda x: x != b, a[start:i])))
start = i + 1
if start < len(a):
res.append(a[start:])
print(res)
Output[[1], [5, 6], [5, 6, 5]]
Explanation: This code loops through a, using filter to remove occurrences of b from each segment and appends the filtered segments to the result. It adds any remaining elements after the last b, splitting the list at b and keeping only non-b elements.
Using reduce
reduce function from functools accumulates results as it processes each element in the list. It builds sublists by appending elements unless a certain condition is met, in which case a new sublist is started. After processing the entire list, empty sublists are removed.
Python
from functools import reduce
a = [1, 4, 5, 6, 4, 5, 6, 5, 4]
b = 4
def accumulate(acc, x):
if x == b:
acc.append([])
else:
acc[-1].append(x)
return acc
res = reduce(accumulate, a, [[]])
# Remove empty sublists
res = [lst for lst in res if lst]
print(res)
Output[[1], [5, 6], [5, 6, 5]]
Explanation: reduce(accumulate, a, [[]]) processes the list a, starting with an empty sublist. The accumulate function creates new sublists whenever x equals b and adds elements to the last sublist when x is not b. The reduce function splits the list at each occurrence of b and a list comprehension removes any empty sublists created at the start or end.
Similar Reads
Python | Split nested list into two lists Given a nested 2D list, the task is to split the nested list into two lists such that the first list contains the first elements of each sublist and the second list contains the second element of each sublist. In this article, we will see how to split nested lists into two lists in Python. Python Sp
5 min read
How to Split Lists in Python? Lists in Python are a powerful and versatile data structure. In many situations, we might need to split a list into smaller sublists for various operations such as processing data in chunks, grouping items or creating multiple lists from a single source. Let's explore different methods to split list
3 min read
Python - Split list into all possible tuple pairs Given a list, the task is to write a python program that can split it into all possible tuple pairs combinations. Input : test_list = [4, 7, 5, 1, 9] Output : [[4, 7, 5, 1, 9], [4, 7, 5, (1, 9)], [4, 7, (5, 1), 9], [4, 7, (5, 9), 1], [4, (7, 5), 1, 9], [4, (7, 5), (1, 9)], [4, (7, 1), 5, 9], [4, (7,
3 min read
Slice till K Dictionary Value Lists - Python The task of slicing the values in a dictionary involves extracting a portion of the list associated with each key, up to a specified index k. This can be achieved by iterating over the dictionary, slicing each list up to the k-th index and storing the result in a new dictionary. For example, given a
4 min read
Python | Group tuple into list based on value Sometimes, while working with Python tuples, we can have a problem in which we need to group tuple elements to nested list on basis of values allotted to it. This can be useful in many grouping applications. Let's discuss certain ways in which this task can be performed. Method #1 : Using itemgetter
6 min read
Python - Convert a list into tuple of lists When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists.For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this con
3 min read