Python sequence of numbers with step
Asked 9 years, 1 month ago Show Viewed 1.6m times How do I create an ascending list between two values? For example, a list between 11 and 16:
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges asked Aug 16, 2013 at 4:43
Use
In Python 3,
Note:
The second number in To increment by steps of
See: How do I use a decimal step value for range()?
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges answered Aug 16, 2013 at 4:47
JaredJared 24.6k7 gold badges52 silver badges61 bronze badges 9 You seem to be looking for
For incrementing by
answered Aug 16, 2013 at 4:46
devnulldevnull 113k33 gold badges227 silver badges216 bronze badges 1 Try:
That is a list in Python 2.x and behaves mostly like a list in Python 3.x. If you are running Python 3 and need a list that you can modify, then use:
ah bon 8,3697 gold badges48 silver badges100 bronze badges answered Aug 16, 2013 at 4:49
Mike HouskyMike Housky 3,8711 gold badge16 silver badges30 bronze badges If you are looking for range like function which works for float type, then here is a very good article.
Output:
answered Mar 15, 2016 at 3:21
Rajesh SuranaRajesh Surana 7831 gold badge9 silver badges15 bronze badges 1 assuming you want to have a range between x to y
use list for 3.x support answered Aug 16, 2013 at 4:47
v2bv2b 1,4169 silver badges14 bronze badges Use list comprehension in python. Since you want 16 in the list too.. Use x2+1. Range function excludes the higher limit in the function.
ah bon 8,3697 gold badges48 silver badges100 bronze badges answered Aug 16, 2013 at 4:46
4 In python you can do this very eaisly
or you can create a recursive function that returns an array upto a given number:
output: ar=[10,9,8,7,6,5,4,3,2,1,0] answered Jun 4, 2019 at 19:01
I got here because I wanted to create a range between -10 and 10 in increments of 0.1 using list comprehension. Instead of doing an overly complicated function like most of the answers above I just did this
By changing the range count to 100 I now get my range of -10 through 10 by using the standard range function. So if you need it by 0.2 then just do range(-200, 200) and so on etc answered Aug 3, 2020 at 21:25
JoseJose 1,49117 silver badges19 bronze badges The most elegant way to do this is by using the
This produces the output:
As expressed before by @Jared, the best way is to use the answered Jan 11, 2018 at 11:22
MichaelMichael 2,2621 gold badge32 silver badges53 bronze badges 2 Every answer above assumes range is of positive numbers only. Here is the solution to return list of consecutive numbers where arguments can be any (positive or negative), with the possibility to set optional step value (default = 1).
For instance, standard command Updated: now step may be negative. Thanks @Michael for his comment. answered Dec 4, 2017 at 15:42
Denis RasulevDenis Rasulev 3,3764 gold badges33 silver badges41 bronze badges 5 While
@Jared's answer for incrementing works for
Instead I needed something like this myself, working not just for
answered Jun 18, 2020 at 13:50
YTZYTZ 7276 silver badges21 bronze badges @YTZ's answer worked great in my case. I had to generate a list from 0 to 10000 with a step of 0.01 and simply adding 0.01 at each iteration did not work due to rounding issues. Therefore, I used @YTZ's advice and wrote the following function:
answered Oct 1, 2021 at 13:13
How do you create a sequence of numbers in Python?Use range() to generate a sequence of numbers
Call range(start, stop) to generate a sequence of numbers from start up to stop . Use a for-loop to iterate over each number in this sequence and use list. append(x) to append the number to an empty list if it meets a certain condition.
What is step in Python range?Syntax of range()
The range of integers ends at stop – 1. step: integer value which determines the increment between each integer in the sequence.
How do I make a list of numbers using range in Python?# Create a list in a range of 10-20. My_list = [ range ( 10 , 20 , 1 )] # Print the list. print (My_list). # Create a list in a range of 10-20. My_list = [ * range ( 10 , 21 , 1 )] # Print the list. print (My_list). How do you make a range inclusive in Python?Inclusive Range
The Python range() function produces a range of values that does not include the last value by default. For example range(0,5) produces a range of values 0, 1, 2, 3, 4. To create an inclusive range, that is, to add the stop value into the range too, add the step value to the stop value.
|