Python Practice › List Programs › Program 56
Split a list at a given length
Write a Python program to split a list into two parts where the first part has a given length.
Input
[4, 6, 7, 3, 2, 5, 6, 7, 6, 4], length 4
Output
[[4, 6, 7, 3], [2, 5, 6, 7, 6, 4]]
Approach
- Two slices at the split point.
Solution
items = [4, 6, 7, 3, 2, 5, 6, 7, 6, 4]n = 4print([items[:n], items[n:]])Output
[[4, 6, 7, 3], [2, 5, 6, 7, 6, 4]]
Learn the concept
New to this topic? Read the lesson, then come back.