﻿ Python: Split a given list into specified sized chunks - w3resource

# Python: Split a given list into specified sized chunks

## Python List: Exercise - 165 with Solution

Write a Python program to split a given list into specified sized chunks.

Sample Solution:

Python Code:

``````def split_list(lst, n):
result = list((lst[i:i+n] for i in range(0, len(lst), n)))
return result
nums = [12,45,23,67,78,90,45,32,100,76,38,62,73,29,83]
print("Original list:")
print(nums)
n = 3
print("\nSplit the said list into equal size",n)
print(split_list(nums,n))
n = 4
print("\nSplit the said list into equal size",n)
print(split_list(nums,n))
n = 5
print("\nSplit the said list into equal size",n)
print(split_list(nums,n))
```
```

Sample Output:

```Original list:
[12, 45, 23, 67, 78, 90, 45, 32, 100, 76, 38, 62, 73, 29, 83]

Split the said list into equal size 3
[[12, 45, 23], [67, 78, 90], [45, 32, 100], [76, 38, 62], [73, 29, 83]]

Split the said list into equal size 4
[[12, 45, 23, 67], [78, 90, 45, 32], [100, 76, 38, 62], [73, 29, 83]]

Split the said list into equal size 5
[[12, 45, 23, 67, 78], [90, 45, 32, 100, 76], [38, 62, 73, 29, 83]]
```

Pictorial Presentation:

Flowchart:

## Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

```print(2_000_000)
```2000000