Python

Python Create a List Which Contains Only Zeros

Users create multiple data structures of different data types in Python, likewise, as integer, string, character, and float. Inserting these data structures requires multiple functions, operators, and methods. Suppose, while programming in Python, you may need to generate a list that takes only zeros. In such a situation, you can use the built-in functions and operators.

This write-up will provide several methods for creating a zeros list in Python.

How to Create a List Which Contains Only Zeros in Python?

Different approaches are used to create a zeros list in Python:

  • “*” Operator
  • “for” Loop
  • repeat()
  • np.zeros()

Method 1: Create a List Which Contains Only Zeros in Python Using “*” Operator

The “*” operator is used to create a zeros list in Python. This operator multiples a list with a particular number then returns a new list.

Syntax

The syntax of the “*” operator is listed below:

c_list = [0] * <num>

 

Here:

  • c_list“ is the list variable.
  • [0]“ is the array/list of zeros.
  • <num>” is the desired number of zeros.

Example

Initially, create a new “c_list” list and initialize with the “0” value at all its “5” indexes. The square brackets are utilized to define the value:

c_list = [0] * 5

 

Use the “print()” statement to get the Python list which contains only zeros:

print(c_list)

 

As you can see, the Python list having all zeros in its five indexes has been displayed successfully:

Method 2: Create a List Which Contains Only Zeros in Python Using “for” Loop

Use the iterative function, such as the “for” loop to create a list which contains only zeros in Python. It is utilized for iterating over a provided sequence. The provided data can be a tuple, a set of instructions, a list, or a string. More specifically, it can be used for creating a list with all zeros in Python.

Syntax

The syntax of the “for” loop is provided below:

c_list = [for i in range(start, end)]

 

Here:

  • c_list“ is a variable used to store a list of zeros.
  • range()” method takes the starting “start” and ending values “end” as parameters.

Example

First, create a list variable with all zeros along with the iterative function “for” loop along with array size starting from “0” and ending at index “5”:

c_list = [0 for i in range(0, 5)]

 

Lastly, print the list of all zeros:

print(c_list)

 

Output

Method 3: Create a List Which Contains Only Zeros in Python Using “repeat()” Method

To generate a list which contains only zeros in Python, the built-in “itertools.repeat()” library function that can be used for repeating a value provided a number of times and the “list()” function for converting an iterative to a list.

Example

First, import a “itertools” library:

import itertools

 

Call the “list()” function that takes the “itertools.repeat()” function along with starting value from “0” and ending at “5”:

c_list = list(itertools.repeat(0, 5))

 

Use the “print()” statement to view the created list of zeros:

print(c_list)

 

Output

Method 4: Create a List Which Contains Only Zeros in Python Using “np.zeros()” Function

Last but not least, a way to create a list that contains only zeros in Python is the “np.zeros()” method that returns an array with the provided number of zeros. Then, the “list()” function can be used to convert the created array into a list.

Example

Initially, import the “numpy” library as “np”:

import numpy as np

 

Call “np.zeros()” function along with the parameters as particular length of the list and its data types passes them to the “c_array_list” list variable:

c_array_list = np.zeros(5, dtype=int)

 

Lastly, to convert the generated zeros array to list, call the “list()” function and get it by using the “print()” statement:

print(list(c_array_list))

 

As you can see, the list of zeros has been generated successfully:

That’s all! We have compiled different methods of generating a list of zeros in Python.

Conclusion

To generate a list of zeros in Python, the built-in multiply “*” operator, the iterative function as “for” loop, the “repeat()”, and “np.zeros()” methods are used. The “*” built-in operator is used to multiply a list with a particular number and then returns a new list. The “for” loop iterates over a sequence. The built-in “itertools.repeat()” library function repeats a value specified a number of times, and the “np.zeros()” method returns an array with the provided number of zeros. This write-up illustrated the techniques for creating a list of zeros in Python.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.