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?
Adding to the amazing answers written above,
To simply create an empty list, supposedly with "zeroes" use,
varunl's currently accepted answer
Works well for non-reference types like numbers. Unfortunately if you want to create a list-of-lists you will run into referencing errors. Example in Python 2.7.6:
As you can see, each element is pointing to the same list object. To get around this, you can create a method that will initialize each position to a different object reference.
There is likely a default, built-in python way of doing this (instead of writing a function), but I'm not sure what it is. Would be happy to be corrected!
Edit: It's
[ [] for _ in range(10)]
Example :
Try this instead:
The above will create a list of size 10, where each position is initialized to
None
. After that, you can add elements to it:Admittedly, that's not the Pythonic way to do things. Better do this:
Or even better, use list comprehensions like this:
(This was written based on the original version of the question.)
All lists can hold as many elements as you like, subject only to the limit of available memory. The only "size" of a list that matters is the number of elements currently in it.
print display s1
is not valid syntax; based on your description of what you're seeing, I assume you meantdisplay(s1)
and thenprint s1
. For that to run, you must have previously defined a globals1
to pass into the function.Calling
display
does not modify the list you pass in, as written. Your code says "s1
is a name for whatever thing was passed in to the function; ok, now the first thing we'll do is forget about that thing completely, and lets1
start referring instead to a newly createdlist
. Now we'll modify thatlist
". This has no effect on the value you passed in.There is no reason to pass in a value here. (There is no real reason to create a function, either, but that's beside the point.) You want to "create" something, so that is the output of your function. No information is required to create the thing you describe, so don't pass any information in. To get information out,
return
it.That would give you something like:
The next problem you will note is that your list will actually have only 9 elements, because the end point is skipped by the
range
function. (As side notes,[]
works just as well aslist()
, the semicolon is unnecessary,s1
is a poor name for the variable, and only one parameter is needed forrange
if you're starting from0
.) So then you end up withHowever, this is still missing the mark;
range
is not some magical keyword that's part of the language the wayfor
anddef
are, but instead it's a function. And guess what that function returns? That's right - a list of those integers. So the entire function collapses toand now you see why we don't need to write a function ourselves at all;
range
is already the function we're looking for. Although, again, there is no need or reason to "pre-size" the list.I'm surprised nobody suggest this simple approach to creating a list of empty lists. This is an old thread, but just adding this for completeness. This will create a list of 10 empty lists
Here's my code for 2D list in python which would read no. of rows from the input :