Write a Python Program to Split a List Every Nth Element

Write a python program to split a list every nth element is provided on this page.

Write A Python Program To Split A List Every Nth Element


numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in range(0, len(numbers_list), 3):
	print(numbers_list[i:i+3])

Here we use the step parameter of range function().

The last two lines can also be written like this.


dump = [print(numbers_list[i:i+3]) for i in range(0, len(numbers_list), 3)]

One liner, Nice! This is printing result using list comprehension.

dump variable will store the result of list comprehensation which is of no use to use.

Related To Python

Comments