w3resource
Python Exercises

Python: Split a list every Nth element

Python List: Exercise - 51 with Solution

Write a Python program to split a list every Nth element.

Python: Split a list every Nth element

Sample Solution:-

Python Code:

C = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
def list_slice(S, step):
    return [S[i::step] for i in range(step)]
print(list_slice(C,3))

Sample Output:

[['a', 'd', 'g', 'j', 'm'], ['b', 'e', 'h', 'k', 'n'], ['c', 'f', 'i', 'l']] 

Flowchart:

Flowchart: Split a list every Nth element

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 sort a list of nested dictionaries.
Next: Write a Python program to compute the difference between two lists.

What is the difficulty level of this exercise?