Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an empty list in Python with certain size

Tags:

python

list

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?

like image 935
Ronaldinho Learn Coding Avatar asked May 23 '12 00:05

Ronaldinho Learn Coding


People also ask

How can we create an empty list in Python?

You can create an empty list using an empty pair of square brackets [] or the type constructor list() , a built-in function that creates an empty list when no arguments are passed. Square brackets [] are commonly used in Python to create empty lists because it is faster and more concise.

How do you assign a value to an empty list in Python?

Create an empty list and insert elements at end using insert() function. Python provides a function insert() i.e. It inserts the item at the given index in list in place.


2 Answers

You cannot assign to a list like lst[i] = something, unless the list already is initialized with at least i+1 elements. You need to use append to add elements to the end of the list. lst.append(something).

(You could use the assignment notation if you were using a dictionary).

Creating an empty list:

>>> l = [None] * 10 >>> l [None, None, None, None, None, None, None, None, None, None] 

Assigning a value to an existing element of the above list:

>>> l[1] = 5 >>> l [None, 5, None, None, None, None, None, None, None, None] 

Keep in mind that something like l[15] = 5 would still fail, as our list has only 10 elements.

range(x) creates a list from [0, 1, 2, ... x-1]

# 2.X only. Use list(range(10)) in 3.X. >>> l = range(10) >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

Using a function to create a list:

>>> def display(): ...     s1 = [] ...     for i in range(9): # This is just to tell you how to create a list. ...         s1.append(i) ...     return s1 ...  >>> print display() [0, 1, 2, 3, 4, 5, 6, 7, 8] 

List comprehension (Using the squares because for range you don't need to do all this, you can just return range(0,9) ):

>>> def display(): ...     return [x**2 for x in range(9)] ...  >>> print display() [0, 1, 4, 9, 16, 25, 36, 49, 64] 
like image 125
varunl Avatar answered Sep 19 '22 12:09

varunl


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)) 
like image 33
Óscar López Avatar answered Sep 19 '22 12:09

Óscar López