364

How can I create a list which contains only zeros? I want to be able to create a zeros list for each int in range(10)

For example, if the int in the range was 4 I will get:

[0,0,0,0]

and for 7:

[0,0,0,0,0,0,0]
Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86
user1040563
  • 5,121
  • 9
  • 34
  • 36

8 Answers8

615
#add code here to figure out the number of 0's you need, naming the variable n.
listofzeros = [0] * n

if you prefer to put it in the function, just drop in that code and add return listofzeros

Which would look like this:

def zerolistmaker(n):
    listofzeros = [0] * n
    return listofzeros

sample output:

>>> zerolistmaker(4)
[0, 0, 0, 0]
>>> zerolistmaker(5)
[0, 0, 0, 0, 0]
>>> zerolistmaker(15)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> 
Tiffany
  • 6,276
  • 1
  • 13
  • 4
  • I guess this is a compilation of some of the other stuff, but it puts it all together, and as the website says, "We're just here to help" (or something like that). – Tiffany Dec 16 '11 at 01:03
  • 29
    just do `return [0] * n`, as `listofzeros` is a filler variable. – yurisich Dec 16 '11 at 04:20
  • 8
    Watch out if you do this with non-primitive types. `[{ }] * 5` will make a list of 5 references to the same dictionary. – Albert Nemec Sep 02 '20 at 23:49
  • This syntax is informal and needs to be upgraded by Python. – user2585501 Aug 23 '21 at 03:12
  • @AlbertNemec This is an important warning. It's easy to assume that `[{}] * 5` is the same as the list comprehension `[{} for i in range(5)]`, and it's not. A trap for beginners (and experienced users too). – AndyB Nov 02 '21 at 07:16
85
$python 2.7.8

from timeit import timeit
import numpy

timeit("list(0 for i in xrange(0, 100000))", number=1000)
> 8.173301935195923

timeit("[0 for i in xrange(0, 100000)]", number=1000)
> 4.881675958633423

timeit("[0] * 100000", number=1000)
> 0.6624710559844971

timeit('list(itertools.repeat(0, 100000))', 'import itertools', number=1000)
> 1.0820629596710205

You should use [0] * n to generate a list with n zeros.

See why [] is faster than list()

There is a gotcha though, both itertools.repeat and [0] * n will create lists whose elements refer to same id. This is not a problem with immutable objects like integers or strings but if you try to create list of mutable objects like a list of lists ([[]] * n) then all the elements will refer to the same object.

a = [[]] * 10
a[0].append(1)
a
> [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]

[0] * n will create the list immediately while repeat can be used to create the list lazily when it is first accessed.

If you're dealing with really large amount of data and your problem doesn't need variable length of list or multiple data types within the list it is better to use numpy arrays.

timeit('numpy.zeros(100000, numpy.int)', 'import numpy', number=1000)
> 0.057849884033203125

numpy arrays will also consume less memory.

Community
  • 1
  • 1
Seppo Erviälä
  • 7,160
  • 7
  • 35
  • 40
  • 1
    Can you explain a bit more why in cases of mutable objects this is a problem? So in integers although they are immutable as the elements refer to the same id won't all the elements be of the same integer? – S. Salman Jul 10 '18 at 17:47
  • 2
    You'd expect that when you create a list of lists that all the lists are different from one another while they're actually references to the same list. This doesn't matter with integers since you cannot change the values of integers (or strings) in Pyhton. When you do operations like `a = a + 1` you get a new `id` for `a + 1` instead of changing the original value at `id(a)`. – Seppo Erviälä Jul 11 '18 at 11:44
  • 2
    The gotcha got me. For creating a list of lists of zeroes (when numpy isn't an option) this was what i came up with: `p_ij = [[0] * k for i in range(k)]` that way it wouldn't share an ID and when you made an assignment such as `p_ij[2][3]` it wouldn't assign it to ALL rows at the 3rd element. – DChaps Apr 03 '21 at 00:50
  • 1
    Thank you. On python3.9, speed gaps are the same. – al.zatv Feb 02 '22 at 22:31
39

The easiest way to create a list where all values are the same is multiplying a one-element list by n.

>>> [0] * 4
[0, 0, 0, 0]

So for your loop:

for i in range(10):
    print [0] * i
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
17
$ python3
>>> from itertools import repeat
>>> list(repeat(0, 7))
[0, 0, 0, 0, 0, 0, 0]
kev
  • 155,172
  • 47
  • 273
  • 272
7

zeros=[0]*4

you can replace 4 in the above example with whatever number you want.

Lelouch Lamperouge
  • 8,171
  • 8
  • 49
  • 60
7
zlists = [[0] * i for i in range(10)]

zlists[0] is a list of 0 zeroes, zlists[1] is a list of 1 zero, zlists[2] is a list of 2 zeroes, etc.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • This is a nice idea, but it doesn't work for lists longer than 9 zeroes. Easily remedied (though it'll never work for arbitrary lists), but then you run into a bigger problem, which is that it stores [T_N](http://en.wikipedia.org/wiki/Triangular_number) zeroes in memory. It's better to use a factory function, as Ben has done in his accepted answer. – Benjamin Hodgson Sep 21 '12 at 22:21
  • 1
    There's another problem with this, which is a slight subtlety due to the way references work: `a=zlists[3]; a.append[5]; b=zlists[3]; print b` outputs `[0, 0, 0, 5]`. `b` is not a list of zeroes, as one might naively expect! – Benjamin Hodgson Sep 21 '12 at 22:24
  • Question was to make a list of lists of zeroes. My answer does that. As for your second "problem" -- you'd have the same problem no matter how you made the list. – kindall Sep 21 '12 at 22:27
  • 2
    `def zeroes(n): return [0]*n` followed by `a=zeroes[3]; a.append[5]; b=zeroes[3]; print b` outputs `[0, 0, 0]`. I was just pointing out to the reader that it doesn't work like a factory. – Benjamin Hodgson Sep 21 '12 at 22:32
5

If you want a function which will return an arbitrary number of zeros in a list, try this:

def make_zeros(number):
    return [0] * number

list = make_zeros(10)

# list now contains: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Gavin Anderegg
  • 6,281
  • 2
  • 25
  • 35
4

Here is the xrange way:

list(0 for i in xrange(0,5)) 
bugfree
  • 49
  • 1