I want to create an empty list (or whatever is the best way) that can hold 10 elements.
After that I want to assign values in that list, for example this is supposed to display 0 to 9:
s1 = list();
for i in range(0,9):
s1[i] = i
print s1
But when I run this code, it generates an error or in another case it just displays []
(empty).
Can someone explain why?
To create a list, just use these brackets: "[]"
To add something to a list, use list.append()
There are two "quick" methods:
It appears that
[None]*x
is faster:But if you are ok with a range (e.g.
[0,1,2,3,...,x-1]
), thenrange(x)
might be fastest: