How to split a list into multiple list using python

featured Image

In this post, we will learn how to split a list into multiple lists of N chunks in Python.

Python provides a simple way to split a list into multiple lists of equal or specified lengths. Splitting a list into sub-lists is useful for breaking up large lists into smaller chunks that are more manageable or better suited for specific purposes.

A List is one of the in-built data types in Python. It is used to store multiple elements in a single variable.

my_list = ['a','b','c']

Now, to split a list into multiple sublists in Python, we have to :

  1. First, find the length of the list
  2. Divide the list by 2, to get the middle index
  3. Use the middle index to divide the list into chunks of equal items.

Let’s split a number list using the above steps.

Split List into Multiple lists in Python

Example:

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

length = len(my_list)

mid_index = length // 2

first_chunk = my_list[:mid_index]
second_chunk = my_list[mid_index:]

print(first_chunk) 
print(second_chunk)

The output is:

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

In the above example, we have used the len() to get the length of the list.

Then to get the middle index we have divided the length by 2 like this length // 2. We find the mid_index as 3.

Next, to get the first chunk, we used my_list[:mid_index]. It means get the items from my_list starting from index 0 up to the mid_index (excluding the item in index 3).

Similarly to get the other half of the list we used my_list[mid_index:], which means get the items from index 3 (mid_index) up to the last index.

We can also use the Python library numpy to split a list into n parts.

Split a List into N parts using Numpy

To split a list into N parts of equal length, we can use the numpy.array_split() function in numpy modules.

The numpy is a Python library that helps us to work with arrays and have a collection of high-level mathematical functions.

Read more about it here: Numpy Documentation

So to divide a list into N parts of sublists we first import numpy and then use the numpy.array_split() function.

Syntax:

numpy.array_split(arr, section)

arr– it’s the list to be split.

section: section we want the list to be divided.

Example:

import numpy as np

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

split_part = np.array_split(my_list, 2)

print(split_part)

The output is:

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

It returns a list with sublists of the equally parted list items.

In the above example, we have split the given list into two parts using np.array_split(my_list, 2).

If you change the section argument to 3, it will split the list into three parts like this

split_part = np.array_split(my_list, 3)
print(split_part)

Output:

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

In conclusion:

In this article, we have learned two methods to split a list into multiple sublists in Python. One method is to manually divide a list into two equal halves by finding the length of the list, calculating the middle index and then slicing the list into two chunks using the middle index.

The second method is to use the numpy.array_split() method to divide a list into a specified number of equal parts. This method from the NumPy library splits the list into the given number of sublists with equal length elements. An example splits a list into 2 and 3 parts using this method.


Related Topics:

Split String and get the first element using python

Get the Index or Position of Item in List in Python

Remove Duplicates from List in Python

Remove the Last N element from a List in Python

Python – Insert an element at specific index in a list

Sort List or Dictionary by two keys in Python

How to flatten nested list in python (5 ways)

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

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