Create empty list with size python

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]]

This post will discuss how to create an empty list with a given size in Python.

To assign any value to a list using the assignment operator at position i, a[i] = x, the list’s size should be at least i+1. Otherwise, it will raise an IndexError, as shown below:

if__name__=='__main__':

    a =[]

    a[0]= 1

    print[a]

    # raise IndexError: list assignment index out of range

Download Code

 
The solution is to create an empty list of None when list items are not known in advance. This can be easily done, as shown below:

if__name__=='__main__':

    a =[None]*5

    print[a]

    # prints [None, None, None, None, None]

Download  Run Code

 
The above code will create a list of size 5, where each position is initialized by None. None is frequently used in Python to represent the absence of a value.

 
Another alternative for creating empty lists with a given size is to use list comprehensions:

if__name__=='__main__':

    a =[Noneforxinrange[5]]

    print[a]

    # prints [[], [], [], [], []]

Download  Run Code

Which solution to use?

The first solution works well for non-reference types like numbers. But you might run into referencing errors in some cases. For example, [[]] * 5 will result in the list containing the same list object repeated 5 times.

if__name__=='__main__':

    a =[[]]*5

    a[0].append[1]

    print[a]

    # prints [[1], [1], [1], [1], [1]]

Download  Run Code

 
The solution to this problem is using list comprehensions like this:

if__name__=='__main__':

    a =[[]forxin range[5]]

    a[0].append[1]

    print[a]

    # prints [[1], [], [], [], []]

Download  Run Code

That’s all about creating an empty list with the given size in Python.

 
Also See:

Initialize a list with the same values in Python

How do I make a list with desired size in Python?

To create a list of n placeholder elements, multiply the list of a single placeholder element with n . For example, use [None] * 5 to create a list [None, None, None, None, None] with five elements None . You can then overwrite some elements with index assignments.

How can we create an empty list in Python?

An empty list in Python can be created in two ways, either by using square brackets [] or by using the list[] constructor.

Can we declare size of list in Python?

Python: Initialize List of Size N Using range[] We can initialize a list of size n using a range[] statement. The difference between using a range[] statement and the None method is that a range[] statement will create a list of values between two numbers.

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

Lists in Python can be created by just placing the sequence inside the square brackets [] . To declare an empty list just assign a variable with square brackets.

Chủ Đề