How do you make a list 0 to 100 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

56.9k11 gold badges117 silver badges137 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

Chủ Đề