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 I make a list of 100 numbers in Python?Ways to create a list from 1 to 100 in Python. Using the range() function to create a list from 1 to 100 in Python.. Using the numpy. arange() function to create a list from 1 to 100 in Python.. Using the for loop with range() to create a list from 1 to 100 in Python.. How do you create a range of numbers in Python?Steps to use range() function. Pass start and stop values to range() For example, range(0, 6) . Here, start=0 and stop = 6 . ... . Pass the step value to range() The step Specify the increment. ... . Use for loop to access each number. Use for loop to iterate and access a sequence of numbers returned by a range() .. How do I print a list of n numbers in Python?Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.
How can you make a list with numbers?To start a numbered list, type 1, a period (.), a space, and some text. Word will automatically start a numbered list for you. Type* and a space before your text, and Word will make a bulleted list. To complete your list, press Enter until the bullets or numbering switch off.
|