Python: How to Initialize List of Size N

Posted in /

Vinay Khatri
Last updated on September 10, 2024

Python list is a dynamic data type. It does not come with a syntax that limits its size. In static programming languages like C, C++, and Java, when we initialize an array, we also need to specify the size of the array. Python list also throws the index error if we try to assign an element to a list at a position that does not exist. The index value of a list starts from 0, and it keeps incrementing by one position when we add a new element to the list.

If you wish to create or initialize a list with a fixed number of elements, there you need to provide that number of elements to the list during initialization. Let's say if you want to create a list of 100 elements, then during list initialization, you need to pass that 100  elements or placeholders to the list. In this Python tutorial, we will discuss two different techniques you can use to initialize a list of size n.

Python: Initialize List of Size N with Multiplication operator

Suppose you want to create a list that holds the top 10 students' Names from a grade based on their score. And for that, you need a list of a fixed length of 10 elements. In that case, you can take the help of the multiplication operator on a list and create a list with 10 placeholder elements. when we multiply a list with an integer number, ``` n ``` we get a new list with n times repeated elements. And if we multiply a list that has a single element, there we get a list with ``` n ``` number of the same elements.

Example

```>>> [1]*10
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

>>> [2,3]*5
[2, 3, 2, 3, 2, 3, 2, 3, 2, 3]

>>> ["Data"] * 10
['Data', 'Data', 'Data', 'Data', 'Data', 'Data', 'Data', 'Data', 'Data', 'Data']

```

Now move to the problem statement, where we need a list that can contain the top 10 students' names. Using the same code structure as shown in the above example, we can initialize a list with 10 placeholder values, then later in the program. We can use that list at any time and assign student names to it.

`top_10_students = ['Unknown'] * 10`
The above statement will create a list with 10 elements having the same value ``` 'Unknown' ``` .
Now we have a list with 10 elements. We can assign values to specific positions using the index number 0 to 9.

Let's say right now we only have the name for the 1st and 10th position holder students. We already have a list with 10 elements. We can easily assign their name to the list.

```top_10_students = ['Unknown']* 10

# assign 1st postion name
top_10_students[0] = "Rahul Jain"

# assign 10th position name
top_10_students[9] = "Gourav Kumar"

print(top_10_students)```
Output
`['Rahul Jain', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Gourav Kumar']`

Python: Initialize List of Size N with range() function

With the ``` range() ``` function, we can also create a list with ``` n ``` numbers of integers. The range() function returns a generator, so we also need to convert it into a list using the list() function. Example

```top_10_students = list(range(10))   #create a list with 10 elements

# assign 1st postion name
top_10_students[0] = "Rahul Jain"

# assign 10th position name
top_10_students[9] = "Gourav Kumar"

print(top_10_students)
```

Output

`['Rahul Jain', 1, 2, 3, 4, 5, 6, 7, 8, 'Gourav Kumar']`

Conclusion

Python list does not have a fixed size. To initialize a list with N size of elements, we need to create a list with that many numbers of elements as placeholders. In many programs, we require such a list and matrix with a fixed number of placeholders. There we can either use the range() function or multiplication operator to create a fixed number of list elements.

People are also reading: