Split List into Multiple Lists in Python – [4 Easy Ways with Examples]

Why do we need to Split list into multiple lists in Python?

  • Breaking a given list of elements into several smaller lists depending on specific criteria is known as list splitting. Several methods, including list comprehensions, loops, or built-in features like groupby, can be used to do this ()..
  • A list can be divided into several sublists for a variety of purposes, including data grouping, parallel processing, data preparation, data filtering, and optimization.

Quick Summary

  • Every method that we are going to discuss in this chapter has its own advantages.
  • The loop slicing and comprehension method is easy to code ,understand and good for data sets with less dimensions.
  • The numpy and itertool modules are used in data analysis where the list size is in millions / billions .They use the most effective way to iterate and modify the values for the lists using various concepts.
  • The numpy arrays are based on the C language concept so it is fast and very reliable to use the numpy module for splitting the list into sublists.
  • Overall using modules like numpy and itertool can save a lot of memory and time for any python program.

Split list into 2 sublists

Splitting the list into 2 sublists can be done easily by slicing the original list to some midpoint (it can be 0 to length of the list ) and storing them in two different list variables.

For Example –

user_list = ["John" , "Mike" , "Tyson" , "Jane" , "Stacy" , "Daisy" ]
midpoint = len(user_list) // 2
user1 = user_list[:midpoint]
user2 = user_list[midpoint:]

print(user1)
print(user2)

Output –

['John', 'Mike', 'Tyson']
['Jane', 'Stacy', 'Daisy']

In the above example we are using the len() function to find the length of the userlist, and the // operator is used to divide and round down the result to the nearest integer. The midpoint index is then used to slice the original list into two sublists: the first sublist contains the elements from the beginning of the list up to the midpoint index, and the second sublist contains elements from the mid-index to the end of the list..

Types of list Splitting

Using for loop and slicing

Splitting the list using the loop and slicing includes following steps –

  • Declare the original list of elements using a variable.
  • Declare the size of the sub-lists that you want to generate in the integer variable.
  • Create an empty list to store the sublists.
  • Use a for loop to iterate over the original list, slicing it into sub-lists of the specified size and appending them to the empty list.

For Example –

def convert(orig_list ,sub_list_size):
    result_sublist = []

    for i in range(0, len(original_list), sub_list_size):
        result_sublist.append(original_list[i:i+sub_list_size])

     return result_sublist

original_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
sublist =convert(original_list ,3)

print(sublist)

Output –

[[10, 20, 30], [40, 50, 60], [70, 80, 90], [100]]

List Splitting using a list comprehension

List comprehension is used to create a new list in Python using a single line of code. it is used to generate a new list by iterating over the existing list and applying user-defined functions.

Syntax –

new_list = [expression for item in iterable if condition]

For Example –

def convert(orig_list ,sub_list_size):
    result_sublist = [original_list[i:i+sub_list_size] for i in range(0, len(original_list), sub_list_size)]
    return result_sublist

original_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
sublist =convert(original_list ,3)
print(sublist)

Output –

[[10, 20, 30], [40, 50, 60], [70, 80, 90], [100]]

The list comprehension method uses a simple for loop to iterate over the original list and create sublists of predefined sub_list_type.

The range() function uses three arguments: start, end, and step to generate a sequence of numbers. The output of slicing is then added to a new list using the list comprehension syntax.

List Splitting Using the numpy.array_split() function

numpy.array_split() is a function inside the NumPy library that splits a given array into multiple sub-lists.

This function is mainly used with large databases where the program has to work with the small chunk of code rather than the whole database

Syntax –

numpy.array_split(original_list, indices_or_sections, axis=0)

original_list – input list that we want to split.

indices_or_sections – an integer or a list of indices that is used to split the list

axis – an optional parameter that indicates the axis along which to split the array.

For Example –

import numpy as np

def convert(orig_list ,sub_list_size):
    return np.array_split(orig_list, sub_list_size)

original_list = np.array(["A", "B" , "C" , "D" , "E" , "F"])
sublist =convert(original_list ,3)

print(sublist)

Output –

[array(['A', 'B'], dtype='<U1'), array(['C', 'D'], dtype='<U1'), array(['E', 'F'], dtype='<U1')]

List Splitting Using zip_longest from itertools

zip_longest() is a function in the itertools module in python that allows you to iterate over multiple lists in parallel time.

it is also used to split lists into smaller sub-lists.

The zip_longest() function is particularly useful when the input lists have different lengths, as it will pad the shorter lists with a specified value (by default None) to make them the same length.

Syntax –

itertools.zip_longest(*iterables, fillvalue=None)

For Example:

import itertools

def convert(orig_list ,sub_list_size):
    return [list(filter(None, sublist)) for sublist in itertools.zip_longest(*([iter(name_list)]*sub_list_size), fillvalue=None)]

name_list = ["John" , "Mike" , "Tyson" , "Jane" , "Stacy" , "Daisy"]
sublist =convert(name_list ,3)
print(sublist)

Output:

[['John', 'Mike', 'Tyson'], ['Jane', 'Stacy', 'Daisy']]

Errors While working with list splitting.

Index out-of-range error:

This error occurs when we try to access an element outside the range of the list.

my_list = [10, 20, 30, 40, 50]
print(my_list[5]) #index 5 is out of range, as my_list has only 5 elements

Output:

IndexError: list index out of range

To fix this error, we can make sure that we are using an index within the range of the list, like this:

my_list = [1, 2, 3, 4, 5]
print(my_list[4]) #prints the last element of my_list

Syntax error:

This error occurs when you have a mistake in the syntax of your code.

print("Hello, world!)

Output:

SyntaxError: EOL while scanning string literal

To fix this error, we can make sure that our code is correctly formatted.

print("Hello, world!")correctly formatted string with closing quotation mark

Output:

Hello, world!

Leave a Reply

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