List of zeros in python

Python

Python Problem Overview


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]

Python Solutions


Solution 1 - Python

#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]
>>> 

Solution 2 - Python

$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.

Solution 3 - Python

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

Solution 4 - Python

$ python3
>>> from itertools import repeat
>>> list(repeat(0, 7))
[0, 0, 0, 0, 0, 0, 0]

Solution 5 - Python

zeros=[0]*4

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

Solution 6 - Python

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.

Solution 7 - Python

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]

Solution 8 - Python

Here is the xrange way:

list(0 for i in xrange(0,5)) 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1040563View Question on Stackoverflow
Solution 1 - PythonTiffanyView Answer on Stackoverflow
Solution 2 - PythonSeppo ErviäläView Answer on Stackoverflow
Solution 3 - PythonAndrew ClarkView Answer on Stackoverflow
Solution 4 - PythonkevView Answer on Stackoverflow
Solution 5 - PythonLelouch LamperougeView Answer on Stackoverflow
Solution 6 - PythonkindallView Answer on Stackoverflow
Solution 7 - PythonGavin AndereggView Answer on Stackoverflow
Solution 8 - PythonbugfreeView Answer on Stackoverflow