92

Say if you had a number input 8 in python and you wanted to generate a list of consecutive numbers up to 8 like

[0, 1, 2, 3, 4, 5, 6, 7, 8]

How could you do this?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
yezi3
  • 963
  • 1
  • 6
  • 4

8 Answers8

156

In Python 3, you can use the builtin range function like this

>>> list(range(9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Note 1: Python 3.x's range function, returns a range object. If you want a list you need to explicitly convert that to a list, with the list function like I have shown in the answer.

Note 2: We pass number 9 to range function because, range function will generate numbers till the given number but not including the number. So, we give the actual number + 1.

Note 3: There is a small difference in functionality of range in Python 2 and 3. You can read more about that in this answer.

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
15

Using Python's built in range function:

Python 2

input = 8
output = range(input + 1)

print output
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Python 3

input = 8
output = list(range(input + 1))

print(output)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Community
  • 1
  • 1
ds_chem
  • 173
  • 6
5

Here is a way to generate n consecutive numbers in equal intervals between them starting from 0 to 100 using numpy:

import numpy as np

myList = np.linspace(0, 100, n)
user15302326
  • 51
  • 1
  • 3
  • 1
    Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes – Ran Marciano Feb 28 '21 at 06:33
  • When you post a piece of code, make sure that it runs. Here, you must define n first. – NoamG Oct 29 '22 at 13:58
2

Just to give you another example, although range(value) is by far the best way to do this, this might help you later on something else.

list = []
calc = 0

while int(calc) < 9:
    list.append(calc)
    calc = int(calc) + 1

print list
[0, 1, 2, 3, 4, 5, 6, 7, 8]
jester112358
  • 465
  • 3
  • 17
1

Note :- Certainly in python-3x you need to use Range function It works to generate numbers on demand, standard method to use Range function to make a list of consecutive numbers is

x=list(range(10))
#"list"_will_make_all_numbers_generated_by_range_in_a_list
#number_in_range_(10)_is_an_option_you_can_change_as_you_want
print (x)
#Output_is_ [0,1,2,3,4,5,6,7,8,9]

Also if you want to make an function to generate a list of consecutive numbers by using Range function watch this code !

def  consecutive_numbers(n) :
    list=[i for i in range(n)]
    return (list)
print(consecutive_numbers(10))

Good Luck!

1

You can use itertools.count() to generate unbounded sequences. (itertools is in the Python standard library). Docs here:
https://docs.python.org/3/library/itertools.html#itertools.count

jsmart
  • 2,921
  • 1
  • 6
  • 13
0

Depending on how you want the result, you can also print each number in a for loop:

def numbers():
    for i in range(int(input('How far do you wanna go? '))+1):
        print(i)

So if the user input was 7 for example:

How far do you wanna go? 7
0
1
2
3
4
5
6
7

You can also delete the '+1' in the for loop and place it on the print statement, which will change it to starting at 1 instead of 0.

StonedTensor
  • 610
  • 5
  • 19
0

You can use list comprehensions for this problem as it will solve it in only two lines.

n = int(input("Enter the range of the list:\n"))
l1 = [i for i in range(n)] #Creates list of numbers in the range 0 to n


print(l1)
Tanish Sarmah
  • 430
  • 5
  • 14