How to declare array of zeros in python (or an array of a certain size)

Question:

I am trying to build a histogram of counts… so I create buckets.
I know I could just go through and append a bunch of zeros i.e something along these lines:

buckets = []
for i in xrange(0,100):
    buckets.append(0)

Is there a more elegant way to do it? I feel like there should be a way to just declare an array of a certain size.

I know numpy has numpy.zeros but I want the more general solution

Asked By: user491880

||

Answers:

You can multiply a list by an integer n to repeat the list n times:

buckets = [0] * 100
Answered By: mjhm

The simplest solution would be

"x00" * size # for a buffer of binary zeros
[0] * size # for a list of integer zeros

In general you should use more pythonic code like list comprehension (in your example: [0 for unused in xrange(100)]) or using string.join for buffers.

Answered By: AndiDog

use numpy

import numpy
zarray = numpy.zeros(100)

And then use the Histogram library function

Answered By: fabrizioM

Depending on what you’re actually going to do with the data after it’s collected, collections.defaultdict(int) might be useful.

Answered By: Russell Borogove

Use this:

bucket = [None] * 100
for i in range(100):
    bucket[i] = [None] * 100

OR

w, h = 100, 100
bucket = [[None] * w for i in range(h)]

Both of them will output proper empty multidimensional bucket list 100×100

Answered By: meeDamian

Just for completeness: To declare a multidimensional list of zeros in python you have to use a list comprehension like this:

buckets = [[0 for col in range(5)] for row in range(10)]

to avoid reference sharing between the rows.

This looks more clumsy than chester1000’s code, but is essential if the values are supposed to be changed later. See the Python FAQ for more details.

Answered By: OK.

Well I would like to help you by posting a sample program and its output

Program :-

t=input("")

x=[None]*t

y=[[None]*t]*t

for i in range(1,t+1):

      x[i-1]=i;
      for j in range(1,t+1):
            y[i-1][j-1]=j;

print x

print y

Output :-

2

[1, 2]

[[1, 2], [1, 2]]

I hope this clears some very basic concept of yours regarding their declaration. To initialize them with some other specific values,like initializing them with 0..you can declare them as :

x=[0]*10

Hope it helps..!! 😉

Answered By: Archit

If you need more columns:

buckets = [[0., 0., 0., 0., 0.] for x in range(0)]
Answered By: renatov

The question says “How to declare array of zeros …” but then the sample code references the Python list:

buckets = []   # this is a list

However, if someone is actually wanting to initialize an array, I suggest:

from array import array

my_arr = array('I', [0] * count)

The Python purist might claim this is not pythonic and suggest:

my_arr = array('I', (0 for i in range(count)))

The pythonic version is very slow and when you have a few hundred arrays to be initialized with thousands of values, the difference is quite noticeable.

Answered By: IAbstract
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.