Search code snippets, questions, articles...

Generate a list of n numbers in Python

Python is a versatile language that you can use to write programs to complete a variety of tasks. In this article, you'll learn how to generate a list of numbers in Python. You'll also learn how to modify the list to meet your needs.
numbers = list(range(5))

print(numbers)

Method #1: Using range() function

Python's range() function is a handy tool for generating a list of consecutive integers. The function takes two arguments: the first is the starting value, and the second is the stopping value.

Syntax

range(START_NUMBER, END_NUMBER)

Parameters

START_NUMBER: The number from where you want to start your list. Default is 0.

END_NUMBER: The number where you want to end your list - 1.

For example, the following code will generate a list of numbers starting at 0 and stopping at 9:

num_list = list(range(10))

print('Generated numbers List', num_list)

Output

Generated numbers List [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

This code example generates a list of numbers from 0 to 9 using the range() function.

The list() function is then used to convert the range object into a list object.

Finally, the generated list is printed on the console.

Code example - Generate a list from 1 to 10

num_list = list(range(1, 11))

print('Generated numbers List', num_list)

Output

Generated numbers List [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

If you want to shuffle the list also then you can read the below article.

Randomly shuffle a list in python

Method #2: Using numpy arange() function

Python's NumPy library has a function called arange() which generates a list of numbers. This is useful for creating lists of numbers for various purposes, such as creating a list of numbers to use in a For loop.

Syntax

import numpy as np

np.arrage(start, end, increment)
Parameters:
start: The number from where you want to start the list. Default 0.
end: Number on which you want to end the list.
increment: The number that you want to add in each step.

Code example: Generate a list from 1 to 10

import numpy as np

num_list = list(np.arange(1, 11))

print(num_list)

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

The numpy.arange() function creates a list of evenly spaced numbers over a specified interval. In this case, the function generates a list of numbers from 1 to 10.

Another example is to generate a number list using np.arrange() function. This time we will use the increment value in each step as 0.4.

import numpy as np

num_list = list(np.arange(.5, 5, .5))

print(num_list)

# [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

The code example above creates a list of numbers from 0.5 to 5 in steps of 0.5.

Method #3: Using List Comprehension

Python's list comprehension is a handy tool for generating lists of numbers. To generate a list of n numbers, simply use the following syntax: [num for num in range(n)]. This will create a list of numbers starting at 0 and going up to (n-1).

start=1
end=10

result = [item for item in range(start, end+1)]

print(result)

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

The above code is an example of list comprehension in Python. A list comprehension is a concise way to create a list in Python.

It consists of square brackets containing an expression followed by a for clause, then zero or more for or if clauses. The expression can be anything, meaning you can put all kinds of objects in lists.

The result will be a new list resulting from evaluating the expression in the context of the for and if clauses that follow it.

In the above code, the expression is "item for item in range(start, end+1)", meaning that each item in the list will be the numbers from start to end (inclusive).

Was this helpful?
0 Comments