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 the position that does not exist. The index value of a list start from 0 and it keeps increment 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 multiple a list which has a single element, there we get a list with
n number of the same elements.
>>> *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 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
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 = "Rahul Jain" # assign 10th position name top_10_students = "Gourav Kumar" print(top_10_students)
['Rahul Jain', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Unknown', 'Gourav Kumar']
Python: Initialize List of Size N with range() function
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.
top_10_students = list(range(10)) #create a list with 10 elements # assign 1st postion name top_10_students = "Rahul Jain" # assign 10th position name top_10_students = "Gourav Kumar" print(top_10_students)
['Rahul Jain', 1, 2, 3, 4, 5, 6, 7, 8, 'Gourav Kumar']
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:
- Python List Methods: All you need to Know
- Replace Item in Python List
- Python List or Array
- How do you remove duplicates from a Python list while preserving order?
- Python List Files in a Directory: A Complete Guide
- List append vs extend method in Python
- Python list vs Tuple
- List Comprehension in Python
- Python List
- How to Declare a List in Python?