0
0
PythonProgramBeginner · 2 min read

Python Program to Split List into Chunks

You can split a list into chunks using a list comprehension like [lst[i:i + n] for i in range(0, len(lst), n)], where lst is your list and n is the chunk size.
📋

Examples

Inputlst = [1, 2, 3, 4, 5], n = 2
Output[[1, 2], [3, 4], [5]]
Inputlst = ['a', 'b', 'c', 'd', 'e', 'f'], n = 3
Output[['a', 'b', 'c'], ['d', 'e', 'f']]
Inputlst = [], n = 4
Output[]
🧠

How to Think About It

To split a list into chunks, think about taking slices of the list starting from the beginning, each slice having the size of the chunk, until you reach the end of the list. Use a loop to move the starting point by the chunk size each time and collect these slices into a new list.
📐

Algorithm

1
Get the input list and chunk size.
2
Start from the first element index 0.
3
Take a slice of the list from the current index to current index plus chunk size.
4
Add this slice to the result list.
5
Move the current index forward by the chunk size.
6
Repeat until the current index reaches or passes the end of the list.
7
Return the list of chunks.
💻

Code

python
def split_list_into_chunks(lst, n):
    return [lst[i:i + n] for i in range(0, len(lst), n)]

# Example usage
my_list = [1, 2, 3, 4, 5]
chunk_size = 2
print(split_list_into_chunks(my_list, chunk_size))
Output
[[1, 2], [3, 4], [5]]
🔍

Dry Run

Let's trace splitting the list [1, 2, 3, 4, 5] into chunks of size 2.

1

Start with i = 0

Take lst[0:2] which is [1, 2]

2

Next i = 2

Take lst[2:4] which is [3, 4]

3

Next i = 4

Take lst[4:6] which is [5]

iChunk taken
0[1, 2]
2[3, 4]
4[5]
💡

Why This Works

Step 1: Using range with step

The range(0, len(lst), n) generates start indexes for each chunk, moving forward by n each time.

Step 2: Slicing the list

Each slice lst[i:i+n] takes a sublist from index i up to but not including i+n, forming one chunk.

Step 3: Collecting chunks

All these slices are collected into a new list, which is the final list of chunks.

🔄

Alternative Approaches

Using a for loop and append
python
def split_list_into_chunks(lst, n):
    chunks = []
    for i in range(0, len(lst), n):
        chunks.append(lst[i:i+n])
    return chunks

print(split_list_into_chunks([1,2,3,4,5], 2))
This method is more explicit and easier to read for beginners but slightly longer.
Using itertools.islice and generator
python
from itertools import islice

def split_list_into_chunks(lst, n):
    it = iter(lst)
    while True:
        chunk = list(islice(it, n))
        if not chunk:
            break
        yield chunk

print(list(split_list_into_chunks([1,2,3,4,5], 2)))
This method uses a generator and is memory efficient for large lists but more complex.

Complexity: O(n) time, O(n) space

Time Complexity

The code visits each element once to create chunks, so it runs in linear time relative to the list size.

Space Complexity

It creates a new list of chunks, so it uses extra space proportional to the input list size.

Which Approach is Fastest?

The list comprehension and for loop methods have similar speed; the generator method is more memory efficient but slightly slower.

ApproachTimeSpaceBest For
List comprehensionO(n)O(n)Simple and fast for most cases
For loop with appendO(n)O(n)Clear and beginner-friendly
Generator with itertools.isliceO(n)O(k) where k is chunk sizeMemory efficient for large lists
💡
Use list slicing with a step in range to split lists cleanly and efficiently.
⚠️
Trying to split the list by modifying it inside the loop, which can cause skipping elements or errors.