How do i make a list of 100 numbers in python?

Create a list of 100 integers whose value and index are the same, e.g.

mylist[0] = 0, mylist[1] = 1, mylist[2] = 2, ...

Here is my code.

x_list=[] def list_append(x_list): for i in 100: x_list.append(i) return(list_append()) print(x_list)

Veedrac

55.7k14 gold badges108 silver badges165 bronze badges

asked Sep 26, 2013 at 3:49

2

Since nobody else realised you're using Python 3, I'll point out that you should be doing list(range(100)) to get the wanted behaviour.

answered Sep 26, 2013 at 4:22

VeedracVeedrac

55.7k14 gold badges108 silver badges165 bronze badges

2

Use range() for generating such a list

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

answered Sep 26, 2013 at 3:50

for i in 100 doesn't do what you think it does. int objects are not iterable, so this won't work. The for-loop tries to iterate through the object given.

If you want to get a list of numbers between 0-100, use range():

for i in range(100): dostuff()

The answer to your question is pretty much range(100) anyway:

>>> range(100)[0] 0 >>> range(100)[64] 64

answered Sep 26, 2013 at 3:50

TerryATerryA

57k11 gold badges117 silver badges138 bronze badges

You can use range(100), but it seems that you are probably looking to make the list from scratch, so there you can use while:

x_list=[] i = 0 while i<100: x_list.append(i) i += 1

Or you could do this recursively:

def list_append(i, L): L.append(i) if i==99: return L list_append(i+1, L) x_list = [] list_append(0, x_list) print x_list

answered Sep 26, 2013 at 3:52

tom10tom10

64.7k9 gold badges122 silver badges134 bronze badges

1

Also can use List Comprehensions, like

[x for x in range(100)]

answered Apr 30, 2018 at 12:58

If you want to import numpy you could do something like this:

import numpy as np x_list = np.arange(0, 100).tolist()

Should work in python2.7 and python3.x

answered Apr 28, 2018 at 18:07

import random data1=[] def f(x): return(random.randrange(0,1000)) for x in range (0,100): data1.append(f(x)) data1

double-beep

4,59613 gold badges30 silver badges40 bronze badges

answered Apr 29, 2020 at 2:47

2

How do you make a list of numbers from 1 to 100 in Python?

To create a list with the numbers from 1 to 100 using Python, we can use the range() function..
list_1_to_100 = range(1, 101) Python..
list_1_to_100 = [] for x in range(1,101): list_1_to_100. append(x) ... .
numbers_1_to_10 = list(range(1,11)) ... .
list_1_to_100 = range(1, 101) ... .
list_1_to_100 = [] for x in range(1,101): list_1_to_100..

How do you make a list of numbers in Python?

Using Python range() Python comes with a direct function range() which creates a sequence of numbers from start to stop values and print each item in the sequence. We use range() with r1 and r2 and then convert the sequence into list.

How do I make a list of numbers in a 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 list of 100 zeros in Python?

Creating a List of Zeros in Python.
list_of_zeros = [0] * 10 print(list_of_zeros) #Output: [0,0,0,0,0,0,0,0,0,0] ... .
list_of_zeros = list(0 for i in range(0,10)) print(list_of_zeros) #Output: [0,0,0,0,0,0,0,0,0,0] ... .
import itertools list_of_zeros = list(itertools..

Chủ đề