# Python – Split list into lists by particular value

## Python | Split list into lists by particular value

The splitting of lists is quite common utility nowadays and there can be many applications and use cases of the same. Along with these always come the variations. One such variation can be split the list by particular value. Let’s discuss a certain way in which list split can be performed.

Method : Using list comprehension + `zip() + slicing + enumerate()`

This problem can be solved in two parts, in first part we get the index list by which split has to be performed using enumerate function. And then we can join the values according to the indices using zip and list slicing.

 `# Python3 code to demonstrate` `# Split list into lists by particular value` `# Using list comprehension + zip() + slicing + enumerate()` ` ` `# initializing list` `test_list ``=` `[``1``, ``4``, ``5``, ``6``, ``4``, ``5``, ``6``, ``5``, ``4``]` ` ` `# printing original list` `print``(``"The original list : "` `+` `str``(test_list))` ` ` `# using list comprehension + zip() + slicing + enumerate()` `# Split list into lists by particular value` `size ``=` `len``(test_list)` `idx_list ``=` `[idx ``+` `1` `for` `idx, val ``in` `            ``enumerate``(test_list) ``if` `val ``=``=` `5``]` ` ` ` ` `res ``=` `[test_list[i: j] ``for` `i, j ``in` `        ``zip``([``0``] ``+` `idx_list, idx_list ``+` `        ``([size] ``if` `idx_list[``-``1``] !``=` `size ``else` `[]))]` ` ` `# print result` `print``(``"The list after splitting by a value : "` `+` `str``(res))`

Output :

```The original list : [1, 4, 5, 6, 4, 5, 6, 5, 4]
The list after splitting by a value : [[1, 4, 5], [6, 4, 5], [6```

Last Updated on March 1, 2022 by admin

## Pandas Dataframe.sort_values()Pandas Dataframe.sort_values()

Python | Pandas Dataframe.sort_values() Python is a great language for doing data analysis, primarily because

## Saving a Video using OpenCVSaving a Video using OpenCV

Saving a Video using OpenCV OpenCV is an open-source and most popular computer vision library

## Interquartile Range and Quartile Deviation using NumPy and SciPyInterquartile Range and Quartile Deviation using NumPy and SciPy

Interquartile Range and Quartile Deviation using NumPy and SciPy Quartiles : A quartile is a

## Python – How to search for a string in text files?Python – How to search for a string in text files?

Python – How to search for a string in text files? In this article, we

## numpy.random.randn() in Pythonnumpy.random.randn() in Python

numpy.random.randn() in Python The numpy.random.randn() function creates an array of specified shape and fills it with random

## Conway’s Game Of Life (Python Implementation)Conway’s Game Of Life (Python Implementation)

Conway’s Game Of Life (Python Implementation) Conways’s Game Of Life is a Cellular Automation Method

## Classifying data using Support Vector Machines(SVMs) in PythonClassifying data using Support Vector Machines(SVMs) in Python

Classifying data using Support Vector Machines(SVMs) in Python Introduction to SVMs: In machine learning, support

## Pooling Layer for Convolutional Neural Networks (CNN)Pooling Layer for Convolutional Neural Networks (CNN)

CNN | Introduction to Pooling Layer The pooling operation involves sliding a two-dimensional filter over