Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Split a List in Python

  • 31 Jul, 2025
  • Com 1
Splitting a List in Python

The best and most efficient way to split a list in Python is to use list slicing. First, calculate the length of the list, determine its middle index, and then divide it into two halves.

Splitting a list using slicing in Python

# Define a list of numbers
main_list = [11, 18, 19, 21, 6, 9, 10, 24]

# Calculate the length of the list
main_list_length = len(main_list)

# Define the middle index for splitting the list
middle_index = main_list_length // 2

# Split the list into two halves
first_part = main_list[:middle_index]
second_part = main_list[middle_index:]

print("first part of the list : ", first_part)
print("second part of the list : ", second_part)

# Output:
# first part of the list : [11, 18, 19, 21]
# second part of the list : [6, 9, 10, 24]

And we have two splitted lists.

Dividing a list involves breaking it down into smaller sublists or elements based on specific criteria, such as size, condition, or delimiters.

Splitting a list into N Equal Parts

You can use list comprehension to break a list into chunks of size N.

Splitting a list into N parts

 

main_list = [11, 18, 19, 21, 6, 9, 10, 24]

print("Before Splitting List:", main_list)
# Output: Before Splitting List: [11, 18, 19, 21, 6, 9, 10, 24]

chunk_size = 3

# Use list comprehension to create chunks of size N
split_parts = [main_list[i:i + chunk_size]
               for i in range(0, len(main_list), chunk_size)]

print("After Splitting List:", split_parts)
# Output: After Splitting List: [[11, 18, 19], [21, 6, 9], [10, 24]]

Splitting by Delimiter

Splitting by Delimiter

To split a list based on a delimiter or separator, you can use the itertools.groupby() method. 

Let’s say we have a list of numbers that includes 0s. We are choosing 0 as our delimiter or separator and splitting the list from 0. So, if 0 appears multiple times, we have multiple chunks in the output.

from itertools import groupby

lst = [1, 2, 0, 3, 4, 0, 5]

# We are choosing 0 as the delimiter to group the list.
groups = [list(group)
          for key, group in groupby(lst, key=lambda x: x != 0) if key]

print(groups)

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

You can see that the output does not contain 0 because that acts as a separator, and it won’t be included in the final list of lists.

Splitting by Condition

What if we want to split a list based on even and odd numbers? That means we must divide the list into two groups. The first group of the list contains even numbers, and the second group of the list contains odd numbers.

We will use list comprehension for this type of condition.

lst = [1, 2, 3, 4, 5, 6]

evens = [x for x in lst if x % 2 == 0]

odds = [x for x in lst if x % 2 != 0]

print("Evens:", evens)
# Output: Evens: [2, 4, 6]

print("Odds:", odds)
# Output: Odds: [1, 3, 5]

Splitting a numpy array

A numpy array differs from a Python list. To split the array, we must convert the list into an array using the np.array() method and then use numpy.array_split() method. It splits the array into equal-sized chunks.

import numpy as np

# Define the list of numbers
main_list = [11, 18, 19, 21, 6, 9, 10, 24]

print("Before Splitting List:", main_list)
# Output: Before Splitting List: [11, 18, 19, 21, 6, 9, 10, 24]

# Convert the list to a NumPy array
main_array = np.array(main_list)

# Split the NumPy array into 3 equal-sized chunks (as equal as possible)
chunked_array = np.array_split(main_array, 3)

# Convert each chunked array to a list of Python ints
split_parts = [[int(x) for x in chunk] for chunk in chunked_array]

# Print the list of chunks
print("After Splitting List:", split_parts)
# Output: After Splitting List: [[11, 18, 19], [21, 6], [9, 10, 24]]

That’s all!

Post Views: 28
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Read and Write a List to a File in Python
Numpy.rot90() Method

1 Comment

  1. Maciek

    October 19, 2021 at 5:02 pm

    I really like the second way with the function. Thanks for the clarification, it seems so obvious now!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend