You are currently viewing Create a List of Zeros in Python

How to create a list of zeros in Python? You can use ‘*‘ multiplication operator to create a list of a desired length, where each element is initialized to zero.

Advertisements

In Python, you can create a list of zeros using many ways, for example, for loop, itertools.repeat(), list comprehension, bytearray, and np.zeros() functions. In this article, I will explain how to create a list of zeros by using all these methods with examples.

1. Quick Examples of Creating a List of Zeros

If you are in a hurry, below are some quick examples of how to create a list of zeros.


# Quick examples of creating alist of zeros

# Example 1: Create a list of zeros 
# Using * operator
zeros_list = [0] * 8

# Example 2: Create a list of zeros 
# Using itertools.repeat() function
zeros_list = list(itertools.repeat(0, 6)

# Example 3: Using itertools.repeat() function
n = 6
zeros_list = list(itertools.repeat(0, n))

# Example 4: Create a list of zeros 
# Using for loop
n = 10
zeros_list = []
for i in range(n):
    zeros_list.append(0)

# Example 5: Using a list comprehension
n = 10  
zeros_list = [0 for i in range(n)]

# Example 6:  Using for loop 
# to generate a list containing zeros
zeros_list = [0 for i in range(0, 10)]

# Example 7: Create a list of zeros 
# Using the bytearray
zeros_list = bytearray(10)
zeros = [int(str(x)) for x in zeros_list]

# Example 8: Using np.zeros() function
mylist = np.zeros(10, dtype=int)

2. Create a List of Zeros Using * Operator

The * operator can be used to create a list of zeros in Python by multiplying a list element with a specific length. For example, I will create a list named zeros_list whose length is 8, where each element is initialized by 0. You can change the length of the list by changing the number that you want to multiply by. Finally, you can get the list of zeros with specified lengths.


# Create a list of zeros 
# Using * operator
zeros_list = [0] * 8
print("List of zeros: ", zeros_list)

Yields below output.

python list zeros

3. Create a List of Zeros Using itertools.repeat() Function

You can also use the itertools.repeat() function to create a list of zeros in Python. For example, itertools.repeat(0, 6) creates an iterator that repeats the value ‘0’ six times, and the list() function converts the iterator to a list. Finally, you can get the list of zeros whose, length is 6.


import itertools
# Create a list of zeros 
# Using itertools.repeat() function
zeros_list = list(itertools.repeat(0, 6))
print("List of zeros: ", zeros_list)

Yields below output.

python list zeros

You can change the length of the list by changing the second argument of itertools.repeat(). So, you can create a list of n zeros using itertools.repeat().


import itertools

# Using itertools.repeat() function
# To create a list of zeros
n = 6
zeros_list = list(itertools.repeat(0, n))
print("List of zeros: ", zeros_list)

Yields the same output as above.

4. Create a List of Zeros Using Loop

You can use a for loop to create a list of zeros. You can use the range() function to create a sequence of n numbers. First, initialize the empty list and the Iterate sequence of 10 numbers using for loop, For every iteration zero will append to the empty list using the append() function. Finally, you print the zeros list to verify that it contains the desired number of zeros.


# Create a list of zeros 
# Using for loop
n = 10

# empty list
zeros_list = []
for i in range(n):
    zeros_list.append(0)
print("List of zeros: ", zeros_list)

# Output:
# List of zeros:  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

5. Create a List of Zeros Using List Comprehension

Alternatively, you can use list comprehension to generate a list of zeros with concise code. The list comprehension consists of the expression 0, which is the value that you want to repeat in the list, followed by the for loop that iterates n times. The resulting list will have n zeros.


# Using a list comprehension
n = 10  
zeros_list = [0 for i in range(n)]
print("List of zeros: ", zeros_list)

# Use the for loop 
# to generate a list containing zeros
zeros_list = [0 for i in range(0, 10)]
print("List of zeros: ", zeros_list)

Yields the same output as above.

6. Create a List of Zeros Using the bytearray

You can create a list of zeros using bytearray, which creates a sequence of 10 zero bytes. Then it converts each byte in the bytearray to an integer by converting it to a string and then using int() to convert the string to an integer. Finally, it creates a list of integers from the converted bytearray by using a list comprehension.


# Create a list of zeros 
# Using the bytearray
zeros_list = bytearray(10)
zeros = [int(str(x)) for x in zeros_list]
print("List of zeros: ", zeros)

Yields the same output as above.

7. Create a List of Zeros Using np.zeros() Function

Finally, You can use the np.zeros() function to create a list of zeros. This function is one of the functions of numpy module of Python. When we want to use this module in our code we must import the numpy module.

Pass length of the list, and specified data type of the list elements(in this case, integers) as the arguments of zeros(). It will return the array of zeros with a specified length. Finally, use the list() function to convert the NumPy array to a list.


import numpy as np

# Using np.zeros() function
mylist = np.zeros(10, dtype=int)
print("List of zeros: ", list(mylist))

Yields the same output as above.

8. Conclusion

In this article, I have explained how to create a list of zeros in Python by using the * operator, itertools.repeat(), for loop, list comprehension, bytearray, and np.zeros() functions with examples.

Happy Learning !!