How to split a list into multiple list using python
To split a list we have to find the len of the list and then divide it by 2 to find the middle index. It helps to split list by chunks size.
In this post, we will learn how to split a list into multiple lists of N chunks in python.
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 :
- First find the length of the list
- Divide the list by 2, to get the middle index
- 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]]
Related Topics:
Related Posts
How to merge JSON files in python
Learn different techniques to merge multiple JSON files in Python using built-in JSON module and Pandas library. Includes examples for combining data arrays and merging dictionaries by key.
Pytube Description and Keyword Not Showing: Solution
Solve the issue with Pytube not showing youtube description and tags in this article.
How to Fix the subprocess-exited-with-error in Python
Learn what cause the subprocess-exited-with-error in Python and also learn how to solve the error in your code.
Python Integer Division: The Floor Division Operator Explained
Article on Python Integer Division operator (//) with examples and the difference between standard division and floor division.
