Python Create List From 1 to N with Step Size

🎯 Problem Formulation: How to create a list from 1 to n with a specified step in Python?

Let’s start with one of the easiest ways: πŸ‘‡

Method 0. Using list() and range()

The Python expression list(range(1, n + 1, step)) generates a list of integers starting from 1 up to and including (n), incrementing by a specified value step.

list(range(1, n + 1, step))

Here, range() is a built-in function that creates a sequence of numbers from its first argument (1) to one less than its second argument ((n+1), making (n) the last number in the range), with the sequence progressing by the value of the third argument (step).

Wrapping range() with list() converts the sequence into a list, which can be used in Python for further operations like iterations, calculations, and more.

I also like the following alternative way: πŸ‘‡

Method 1. Using range() in List Comprehension

Using [i for i in range(1, n+1, step)] is the most straightforward method using built-in Python functions: list comprehension and range().

list_with_step = [i for i in range(1, n + 1, step)]

Method 2. Using numpy.arange()

For those using the NumPy library, particularly effective for large data sets due to its optimized performance.

import numpy as np
list_with_step = np.arange(1, n + 1, step).tolist()

Method 3. Using List Slicing

Generate a full list first and then use slicing to select every stepth element.

list_with_step = list(range(1, n + 1))[::step]

The expression list(range(1, n + 1))[::step] first creates a list of integers from 1 to ( n ) using range() and list(). It then uses slicing [::-step] to return every stepth element from the generated list. This approach is efficient for selecting spaced elements from an already defined sequence.

Method 4. Using itertools.islice with itertools.count

Utilizes lazy evaluation, ideal for very large ranges.

from itertools import islice, count
list_with_step = list(islice(count(1, step), (n - 1) // step + 1))

Method 5. Using a for loop with Index Management

Explicitly controls the index, providing clarity at the cost of more verbose code.

list_with_step = []
for i in range(1, n + 1):
    if (i - 1) % step == 0:
        list_with_step.append(i)

For example:

Method 6. Using filter with range

Applies a filter function to a range, extracting elements based on a condition.

list_with_step = list(filter(lambda x: (x - 1) % step == 0, range(1, n + 1)))

Method 7. Using a while loop

This method manually controls the addition of elements to the list.

list_with_step = []
i = 1
while i <= n:
    list_with_step.append(i)
    i += step

Method 8. Using itertools.takewhile with itertools.count

Combines two itertools functions to generate a controlled sequence that stops when a condition is met.

from itertools import takewhile, count
list_with_step = list(takewhile(lambda x: x <= n, count(1, step)))

Method 9. Using map and range

Incorporates a transformation directly within the map function for adjusting step intervals.

list_with_step = list(map(lambda x: 1 + (x * step), range((n + step - 1) // step)))

Method 10. Advanced Conditional List Comprehension

Incorporates condition checks directly within a list comprehension for greater control.

list_with_step = [i for i in range(1, n + 1) if (i - 1) % step == 0]

Also check out my related article:

πŸ‘‰ Python Create List From 1 to N