Python Practice › List Programs › Program 56

Split a list at a given length

EasyList Programs · 56 of 100

Write a Python program to split a list into two parts where the first part has a given length.

Advertisement
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

  1. Two slices at the split point.

Solution

Python
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.

More list programs

← All 100 List Programs

Leave a Comment