• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Creating a List of Zeros in Python

Creating a List of Zeros in Python

February 12, 2022 Leave a Comment

In Python, we can easily create a list of zeros. The easiest way to create a list of zeros only is to use the Python * operator.

list_of_zeros = [0] * 10

print(list_of_zeros)

#Output:
[0,0,0,0,0,0,0,0,0,0]

A second way to make a list of zeros in Python is to use a for loop.

list_of_zeros = list(0 for i in range(0,10))

print(list_of_zeros)

#Output:
[0,0,0,0,0,0,0,0,0,0]

Finally, we can create a list of list of zeros using the Python itertools repeat() function.

import itertools

list_of_zeros = list(itertools.repeat(0,10))

print(list_of_zeros)

#Output:
[0,0,0,0,0,0,0,0,0,0]

In Python, creating a list of zeros can be useful if we want to initialize a list to count or fill later on in our program.

There are a number ways that we can create and fill a list with zeros.

The easiest way to create a list with only zeros is to use the * operator on a single item array containing 0.

To get a list of 10 zeros for example, we multiply the single item list by 10.

list_of_zeros = [0] * 10

print(list_of_zeros)

#Output:
[0,0,0,0,0,0,0,0,0,0]

You can use this method to create a list which contains any value as shown below in Python.

list_of_a = ["a"] * 10

print(list_of_a)

#Output:
["a","a","a","a","a","a","a","a","a","a"]

Filling a List With Zeros Using a Loop in Python

Another method to creating a list of zeros is to use a loop in your Python code.

Below shows how you can fill a list with only zeros using a loop in Python.

list_of_zeros = list(0 for i in range(0,10))

print(list_of_zeros)

#Output:
[0,0,0,0,0,0,0,0,0,0]

Making a List With Zeros Using the Python itertools repeat() Function

The Python itertools module is very useful and gives us a number of great functions to work with iterable objects.

We can use the itertools repeat() function to create a list of zeros in Python as shown below.

import itertools

list_of_zeros = list(itertools.repeat(0,10))

print(list_of_zeros)

#Output:
[0,0,0,0,0,0,0,0,0,0]

Hopefully this article has been useful for you to learn how to make a list with only zeros in Python.

Other Articles You'll Also Like:

  • 1.  Draw Rectangle in Python Using turtle Module
  • 2.  Print Object Attributes in Python using dir() Function
  • 3.  How to Draw a Triangle in Python Using turtle Module
  • 4.  Remove Element from Set in Python
  • 5.  Count Number of Files in Directory with Python
  • 6.  Intersection of Two Lists in Python
  • 7.  Read Pickle Files with pandas read_pickle Function
  • 8.  Sum Columns Dynamically with pandas in Python
  • 9.  Python asin – Find Arcsine and Inverse Sine of Number Using math.asin()
  • 10.  Using Python to Insert Item Into List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy