w3resource
Python Exercises

Python: Generate groups of five consecutive numbers in a list

Python List: Exercise - 44 with Solution

Write a Python program to generate groups of five consecutive numbers in a list.

Python: Generate groups of five consecutive numbers in a list

Sample Solution:-

Python Code:

l = [[5*i + j for j in range(1,6)] for i in range(5)]
print(l)

Sample Output:

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]  

Flowchart:

Flowchart: Generate groups of five consecutive numbers in a list

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.

Previous: Write a Python program to split a list into different variables.
Next: Write a Python program to convert a pair of values into a sorted unique array.

What is the difficulty level of this exercise?