Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Split Lists in Python?
Next article icon

Split list into lists by value – Python

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Using itertools.groupby

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.




Next Article
How to Split Lists in Python?
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • python
  • Python list-programs
Practice Tags :
  • python
  • python

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 co
    3 min read
  • Split a Python List into Sub-Lists Based on Index Ranges
    The task of splitting a Python list into sub-lists based on index ranges involves iterating through a list of index pairs and extracting the corresponding slices. Given a list a and a list of tuples b, where each tuple represents a start and end index, the goal is to create sub-lists that include el
    3 min read
  • Python - Split List on next larger value
    Given a List, perform split on next larger value. Input : test_list = [4, 2, 3, 7, 5, 1, 3, 4, 11, 2] Output : [[4, 2, 3], [7, 5, 1, 3, 4], [11, 2]] Explanation : After 4, 7 is greater, split happens at that element, and so on. Input : test_list = [4, 2, 3, 7, 5, 1, 3, 4, 1, 2] Output : [[4, 2, 3],
    2 min read
  • Python | Split list in uneven groups
    Sometimes, while working with python, we can have a problem of splitting a list. This problem is quite common and has many variations. Having solutions to popular variations proves to be good in long run. Let's discuss certain way to split list in uneven groups as defined by other list. Method 1: Us
    6 min read
  • Python | Convert list of tuples into list
    In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences