Appearance
How do I create a list with numbers between two values?
Here is an example of how you might create a list of numbers between two values in python:
Create a list of numbers between two values in Python
python
start = 5
end = 10
my_list = list(range(start, end))
print(my_list)
# Output: [5, 6, 7, 8, 9]In this example, the range(start, end) function generates a sequence of numbers from start up to, but not including, end. The list() function is then used to convert the range to a list.
Alternatively, a list comprehension can also be used to achieve this:
Create a list of numbers between two values in Python using list comprehension
python
start = 5
end = 10
my_list = [x for x in range(start,end)]
print(my_list)You can also generate a list of numbers with a specific step between each number using range function
Create a list of numbers between two values in Python by generating a list of numbers first
python
my_list = list(range(0, 10, 2))
print(my_list)Where step is the value to increment.