def splitlist(L):
    if not L: return []
    answer = [[L[0]]]

    for i in L[1:]:
        if i - answer[-1][-1] < 4:
            answer[-1].append(i)
        else:
            answer.append([i])
    return answer

Output:

In [112]: splitlist([1,2,3,9,10,11,100,200])
Out[112]: [[1, 2, 3], [9, 10, 11], [100], [200]]
Answer from inspectorG4dget on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-split-list-into-lists-by-particular-value
Split list into lists by value - Python - GeeksforGeeks
July 11, 2025 - 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.
Top answer
1 of 3
5

Use a dictionary for a variable number of variables.

In this case, you can use itertools.groupby to efficiently separate your lists:

L = ['abcd 1233','cdgfh3738','hryg21','**L**',
     'gdyrhr657','abc31637','**R**','7473hrtfgf']

from itertools import groupby

# define separator keys
def split_condition(x):
    return x in {'**L**', '**R**'}

# define groupby object
grouper = groupby(L, key=split_condition)

# convert to dictionary via enumerate
res = dict(enumerate((list(j) for i, j in grouper if not i), 1))

print(res)

{1: ['abcd 1233', 'cdgfh3738', 'hryg21'],
 2: ['gdyrhr657', 'abc31637'],
 3: ['7473hrtfgf']}
2 of 3
2

Consider using one of many helpful tools from a library, i.e. more_itertools.split_at:

Given

import more_itertools as mit


lst = [
    "abcd 1233", "cdgfh3738", "hryg21", "**L**",
    "gdyrhr657", "abc31637", "**R**", 
    "7473hrtfgf"
]

Code

result = list(mit.split_at(lst, pred=lambda x: set(x) & {"L", "R"}))

Demo

sublist_1, sublist_2, sublist_3 = result

sublist_1
# ['abcd 1233', 'cdgfh3738', 'hryg21']
sublist_2
# ['gdyrhr657', 'abc31637']
sublist_3
# ['7473hrtfgf']

Details

The more_itertools.split_at function splits an iterable at positions that meet a special condition. The conditional function (predicate) happens to be a lambda function, which is equivalent to and substitutable with the following regular function:

def pred(x):
    a = set(x)
    b = {"L", "R"}
    return a.intersection(b)

Whenever characters of string x intersect with L or R, the predicate returns True, and the split occurs at that position.

Install this package at the commandline via > pip install more_itertools.

🌐
GitHub
gist.github.com › seanpianka › 1e65b89e5e09f245a07f4d668cb3a44c
Python: create sublist by condition (Split a Python list into a list of lists where the lists are split around elements matching a certain criteria) · GitHub
Python: create sublist by condition (Split a Python list into a list of lists where the lists are split around elements matching a certain criteria) - yield_subgroups.py
🌐
Quora
quora.com › How-do-you-split-a-list-into-multiple-lists-in-Python-1
How to split a list into multiple lists in Python - Quora
Answer (1 of 6): My solution below is in Plain English rather than Python, but Plain English reads like pseudocode so you should be able to translate it easily enough. I started by scratching out a design on a napkin, where the list of hobbies too long to complete in a single day on the left is ...
Find elsewhere
🌐
Python Forum
python-forum.io › thread-18639.html
splitting numeric list based on condition
May 25, 2019 - I am trying to split a list of numbers into sublists once a condition is met. num_list = [0,1,2,3,4,5,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5] Whenever the list reaches 5, it needs to be splitted as a sublist resulting as below: [[0,1,2,3,4,5],[2,3,4,5],...
Top answer
1 of 4
9

I've quickly written one way to do this, I'm sure there are more efficient ways, but this works at least:

num_list =[97, 122, 99, 98, 111, 112, 113, 100, 102]

arrays = [[num_list[0]]] # array of sub-arrays (starts with first value)
for i in range(1, len(num_list)): # go through each element after the first
    if num_list[i - 1] < num_list[i]: # If it's larger than the previous
        arrays[len(arrays) - 1].append(num_list[i]) # Add it to the last sub-array
    else: # otherwise
        arrays.append([num_list[i]]) # Make a new sub-array 
print(arrays)

Hopefully this helps you a bit :)

2 of 4
6

Here is a one-linear Numpythonic approach:

np.split(arr, np.where(np.diff(arr) < 0)[0] + 1)

Or a similar approach to numpy code but less efficient:

from operator import sub
from itertools import starmap
indices = [0] + [
                  i+1 for i, j in enumerate(list(
                        starmap(sub, zip(num_list[1:], num_list)))
                    ) if j < 0] + [len(num_list)
                ] + [len(num_list)]

result = [num_list[i:j] for i, j in zip(indices, indices[1:])]

Demo:

# Numpy
In [8]: np.split(num_list, np.where(np.diff(num_list) < 0)[0] + 1)
Out[8]: 
[array([ 97, 122]),
 array([99]),
 array([ 98, 111, 112, 113]),
 array([100, 102])]

# Python
In [42]: from operator import sub

In [43]: from itertools import starmap

In [44]: indices = [0] + [i+1 for i, j in enumerate(list(starmap(sub, zip(num_list[1:], num_list)))) if j < 0] + [len(num_list)]

In [45]: [num_list[i:j] for i, j in zip(indices, indices[1:])]
Out[45]: [[97, 122], [99], [98, 111, 112, 113], [100, 102]]

Explanation:

Using np.diff() you can get the differences of each item with their next item (up until the last element). Then you can use the vectorized nature of numpy to get the indices of the places where this difference is negative, which can be done with a simple comparison and np.where(). Finally you can simply pass the indices to np.split() to split the array based on those indices.

🌐
DataCamp
datacamp.com › tutorial › python-split-list
How to Split Lists in Python: Basic and Advanced Methods | DataCamp
June 21, 2024 - For loops can also be used to split lists in Python based on conditions and through iteration. # Define a function to split a list into sub-lists of size n def split_by_n(lst, n): # Use a list comprehension to create sub-lists # For each index ...
🌐
sqlpey
sqlpey.com › python › python-list-partitioning-methods
Python: Efficiently Partitioning a List Based on a Condition …
November 4, 2025 - The itertools.tee function can duplicate an iterator, allowing both conditions to pull from the same source without fully materializing intermediate lists. from itertools import tee def split_on_condition_lazy(sequence, condition_func): # Tee duplicates the input iterator l1, l2 = tee((condition_func(item), item) for item in sequence) # Generator 1: True results true_gen = (i for p, i in l1 if p) # Generator 2: False results false_gen = (i for p, i in l2 if not p) return true_gen, false_gen # Example usage (generates results only when consumed) primes_gen, non_primes_gen = split_on_condition_lazy(count(), is_prime) # Consume the first 5 of each: # print("Primes:", list(islice(primes_gen, 5)))
🌐
Delft Stack
delftstack.com › home › howto › python › python split list into multiple lists
How to Split List Into Sublists in Python | Delft Stack
February 2, 2024 - itertools.groupby offers great flexibility, as you can define the key_func to specify the criteria for grouping elements. You can use more complex key functions for more specific grouping requirements, allowing you to split lists into sublists based on various conditions.
🌐
Thetopsites
thetopsites.net › article › 52585545.shtml
Python: split a list based on a condition?
Slicing a list into sublists based ... python-split; It’s installable normally via PyPI: pip install --user split To split a list base on condition, use partition function:...
🌐
Ned Batchelder
nedbatchelder.com › blog › 201306 › filter_a_list_into_two_parts
Filter a list into two parts | Ned Batchelder
June 11, 2013 - def iterpartition(pred, it): """Partition an iterable based on a predicate. Returns two iterables, for those with pred False and those True.