Initialize a List of Any Size with Specific Values in Python
Last Updated :
19 Nov, 2024
In Python, we often need to create a list with a specific size and initialize all elements with the same value. This is useful when we want to set up a list but don't yet have the exact values we need. There are several ways to do this.
Let’s start with the simplest way to create a list with repeated values using Python.
Using Multiplication ( * )
One of the easiest and most common ways to initialize a list with a specific value is by using multiplication. This method is quick and simple.
Python
n = 5
val = 0
# Initialize a list of size n with all elements as 0
lst = [val] * n
print(lst)
[x] * n creates a list with n elements, all set to the value of x. In this case, the list will have 5 zeros.
Other methods that can be used to initialize list with a specific value is:
Using List Comprehension
In List comprehension is another powerful feature in Python, and it lets us create lists dynamically by iterating over a sequence. While it's most commonly used for transforming data, it can also be used for initializing lists.
Example:
Python
# Initialize a list with 5 elements, all set to zero
a = [0 for _ in range(5)]
# another example
b = [i**2 for i in range(5)]
print(a)
print(b)
Using the * Operator with Objects
When we use *
to multiply lists of mutable objects Python doesn’t actually create new independent lists. Instead, it creates references to the same list. So when you modify one inner list, all other references to it change as well.
Python
# Initialize a list with 3 empty lists
a = [[]] * 3
print(a)
a[0].append(1)
print(a)
Output[[], [], []]
[[1], [1], [1]]
To avoid this problem, you can use list comprehension.
Python
# Initialize a list of 3 distinct empty lists
a = [[] for _ in range(3)]
a[0].append(1)
print(a)
# Output: [[1], [], []]
The itertools.repeat
function in Python is another way to initialize a list with a specific value. It allows you to repeat a value multiple times, which can be useful for creating large lists efficiently.
Example:
Python
import itertools
# Initialize a list with 5 elements, all set to 'hello'
a = list(itertools.repeat('geeks', 5))
print(a)
Output['hello', 'hello', 'hello', 'hello', 'hello']
Similar Reads
How to Take a List as Input in Python Without Specifying Size? In many situations, we might want to take list as input without knowing the size in Python beforehand. This approach provides flexibility by allowing users to input as many elements as they want until a specified condition (like pressing Enter) is met.Letâs start with the most simple method to take
2 min read
Python - Initialize dictionary with custom value list In python one usually comes across situations in which one has to use dictionary for storing the lists. But in those cases, one usually checks for first element and then creates a list corresponding to key when it comes. But its always wanted a method to initialize the dict. keys with a custom list.
4 min read
Python - Create a Dictionary using List with None Values The task of creating a dictionary from a list of keys in Python involves transforming a list of elements into a dictionary where each element becomes a key. Each key is typically assigned a default value, such as None, which can be updated later. For example, if we have a list like ["A", "B", "C"],
3 min read
Python - Ways to find indices of value in list In Python, it is common to locate the index of a particular value in a list. The built-in index() method can find the first occurrence of a value. However, there are scenarios where multiple occurrences of the value exist and we need to retrieve all the indices. Python offers various methods to achi
3 min read
List As Input in Python in Single Line Python provides several ways to take a list as input in Python in a single line. Taking user input is a common task in Python programming, and when it comes to handling lists, there are several efficient ways to accomplish this in just a single line of code. In this article, we will explore four com
3 min read
Python | Split a list into sublists of given lengths The problem of splitting a list into sublists is quite generic but to split in sublist of given length is not so common. Given a list of lists and list of length, the task is to split the list into sublists of given length. Example: Input : Input = [1, 2, 3, 4, 5, 6, 7] length_to_split = [2, 1, 3, 1
2 min read