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
(This was written based on the original version of the question.)
I want to create a empty list (or whatever is the best way) can hold 10 elements.
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.
but when I run it, the result is []
print display s1
is not valid syntax; based on your description of what you're seeing, I assume you meant display(s1)
and then print s1
. For that to run, you must have previously defined a global s1
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 let s1
start referring instead to a newly created list
. Now we'll modify that list
". 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:
def display():
s1 = list();
for i in range(0, 9):
s1[i] = i
return s1
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 as list()
, the semicolon is unnecessary, s1
is a poor name for the variable, and only one parameter is needed for range
if you're starting from 0
.) So then you end up with
def create_list():
result = list()
for i in range(10):
result[i] = i
return result
However, this is still missing the mark; range
is not some magical keyword that's part of the language the way for
and def
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 to
def create_list():
return range(10)
and 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.
Make it more reusable as a function.
def createEmptyList(length,fill=None):
'''
return a (empty) list of a given length
Example:
print createEmptyList(3,-1)
>> [-1, -1, -1]
print createEmptyList(4)
>> [None, None, None, None]
'''
return [fill] * length
This code generates an array that contains 10 random numbers.
import random
numrand=[]
for i in range(0,10):
a = random.randint(1,50)
numrand.append(a)
print(a,i)
print(numrand)
Try this instead:
lst = [None] * 10
The above will create a list of size 10, where each position is initialized to None
. After that, you can add elements to it:
lst = [None] * 10
for i in range(10):
lst[i] = i
Admittedly, that's not the Pythonic way to do things. Better do this:
lst = []
for i in range(10):
lst.append(i)
Or even simpler, in Python 2.x you can do this to initialize a list with values from 0 to 9:
lst = range(10)
And in Python 3.x:
lst = list(range(10))
Here's my code for 2D list in python which would read no. of rows from the input :
empty = []
row = int(input())
for i in range(row):
temp = list(map(int, input().split()))
empty.append(temp)
for i in empty:
for j in i:
print(j, end=' ')
print('')
varunl's currently accepted answer
>>> l = [None] * 10
>>> l
[None, None, None, None, None, None, None, None, None, None]
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:
>>> a = [[]]*10
>>> a
[[], [], [], [], [], [], [], [], [], []]
>>> a[0].append(0)
>>> a
[[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]
>>>
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.
def init_list_of_objects(size):
list_of_objects = list()
for i in range(0,size):
list_of_objects.append( list() ) #different object reference each time
return list_of_objects
>>> a = init_list_of_objects(10)
>>> a
[[], [], [], [], [], [], [], [], [], []]
>>> a[0].append(0)
>>> a
[[0], [], [], [], [], [], [], [], [], []]
>>>
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 :
>>> [ [random.random() for _ in range(2) ] for _ in range(5)]
>>> [[0.7528051908943816, 0.4325669600055032], [0.510983236521753, 0.7789949902294716], [0.09475179523690558, 0.30216475640534635], [0.3996890132468158, 0.6374322093017013], [0.3374204010027543, 0.4514925173253973]]