Python Create a List of Size N with the Same Value

python create a list of size n

When we look into Python, there are multiple ways to create a list of a particular size with the same value. We need this kind of setting for carrying out operations in bulk. Let’s go through all the different techniques for creating a list of size N with same value.

Using a loop to create a List of Size N with the Same Value

We can use a simple loop which will iterate and append the required value to the list. This approach is straightforward.
N = 5
value = 1  
my_list = []
for _ in range(N):
    my_list.append(value)
print(my_list) 
output
[1, 1, 1, 1, 1]
N variable stores value to determine how many time value “value” needs to be added in the list (my_list).In the above code we are trying to append value 1 five times.

Using list comprehension

We can use list comprehension to create a list of size N with the same value.
N = 5
value = 0 
my_list = [value for _ in range(N)]
print(my_list)  
output
[0, 0, 0, 0, 0]
‘value’ variable is storing value that is going to added N times. The code : [value for _ in range(N)] creates a list where each element ‘value’ is repeated ‘N’ times.

Using Multiplication operator to create a List of Size N with the Same Value

Another way of creating a list of size N with the same value is by using the multiplication operator (*).
N = 5
value = 2  
my_list = [value] * N
print(my_list) 

output
[2, 2, 2, 2, 2]
The above code creates a list having 5 items . Each item value is 2. This method works well for initializing lists with primitive types like integer and string.

Using Numpy to create a List of Size N with the Same Value

To use Numpy,  we have to first install Numpy.. To install numpy write the following command in cmd “pip install numpy” and execute it by hitting the enter button. Then you have to import numpy in the code file. Now, let’s explore the code
import numpy as np
N = 5
value = 3
my_list = np.full(N, value).tolist()
print(my_list)  
output
[3, 3, 3, 3, 3]
N variable represents number of times the value needs to be added into the list. Function np.full(N, value) created a Numpy array of length N with Value The .tolist() function converts the NumPy array to a Python list. We can also use np.zeros and np.ones function for creating a matrix of zeros and ones respectively.To create a list of zeros you can follow the below code.
import numpy as np
dimensions=(1,4)
zero_array=np.zeros(dimensions)
zero_list=zero_array.tolist()
print('Python list having four zeros',zero_list) 
output
Python list having four zeros [[0.0, 0.0, 0.0, 0.0]]
The np.zeros() function creates a matrix of zeros. We just need to pass the dimension of matrix we want. On a similar basis, let’s explore the code for creating a list of ones.
import numpy as np
dimensions=(2,4)
one_array=np.ones(dimensions)
one_list=one_array.tolist()
print('Python list having four one',one_list) 
output
Python list having four one [[1.0, 1.0, 1.0, 1.0], 
                            [1.0, 1.0, 1.0, 1.0]]
The above code creates a array of (2 x 4) containing value 1 which is converted into list using tolist() function.

Using map and lambda function

>We can use the map function with the lambda function to create a list. Let us go through a  example

n_iteration = 5
val = 4  # Example value
my_list = list(map(lambda x: val, range(N)))
print(my_list)

output

[4, 4, 4, 4, 4]

Let’s break down the code.

  • The variable n_iteration tells the number of times the elements need to be added. Val variable stores value to be added inside the list.
  • The range function generates the sequence from 0 to N-1. For example if range(5) then the sequence generated will be 0,1,2,3,4.
  • In the code map(lambda x: val, range(N)) map function is used to apply a lambda function to each value coming from the range function. Every time the lambda function is called it gives val i.e. 4.
  • The code is enclosed in list() which converts the iterator produced by the map function into the list.

Learn about lambda function

Using `itertools.repeat`

You can use `itertools.repeat` to repeat the value `N` times and convert it to a list. Let’s go through the code
import itertools

N = 5
value = 6 
my_list = list(itertools.repeat(value, N))
print(my_list) 
output
[6, 6, 6, 6, 6]
The code list(itertools.repeat(value, N)) uses itertools.repeat to repeat ‘value’ ‘N’ times then convert the result to a list. After that we print the list (my_list).

Conclusion

We saw 6 different was in Python to create a list of size N with same value. You need to choose a method that fits best with required condition. The methods containing numpy (np.full) and itertools.repeat are particularly useful when dealing with larger datasets or when concerned with performance.