Python Practice › List Programs › Program 46

Groups of five consecutive numbers

EasyList Programs · 46 of 100

Write a Python program to create lists of five consecutive numbers from 1 to 20.

Advertisement
Output
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]

Approach

  1. Slice range(1, 21) in steps of 5.

Solution

Python
numbers = list(range(1, 21))print([numbers[i:i + 5] for i in range(0, len(numbers), 5)])
Output
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Leave a Comment