Have a look at the array module:

import array
array.array('B', [0] * 10000)

Instead of passing a list to initialize it, you can pass a generator, which is more memory efficient.

Answer from unbeknown on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.digitize.html
numpy.digitize — NumPy v2.5 Manual
numpy.digitize is implemented in terms of numpy.searchsorted. This means that a binary search is used to bin the values, which scales much better for larger number of bins than the previous linear search. It also removes the requirement for the input array to be 1-dimensional.
Discussions

binning data in python with scipy/numpy - Stack Overflow
Copyimport numpy as np def ... array "arrx" (typically this could be a time series, or a temperature series etc...), and take a series of bins, then find which bin does every position in arrx belong to, then for each bin average the corresponding values in arry all arry in "*args" must have the same length as arrx efficient arbitrary_averager as no loop over python list is used ... More on stackoverflow.com
🌐 stackoverflow.com
Converting integer array to binary array in python - Stack Overflow
I am trying to convert an array with integers to binary, using python 2.7. More on stackoverflow.com
🌐 stackoverflow.com
Fill array with binary numbers
Is this what you want? a = [list(bin(i).split('b')[1].zfill(5)) for i in range(32)] More on reddit.com
🌐 r/learnpython
10
2
June 12, 2019
arrays - Is there a way to create bins in python instead of listing all the bin numbers (as seen in code below), and maybe without having to use np.digitize? - Stack Overflow
In my code, I have created 10 bins (specific ranges of bins are listed below): 4100000-4155304 4155304-4210608 4210608-4321216 4321216-4542432 4542432-4984865 4984865-5327533 5327533-5670201 More on stackoverflow.com
🌐 stackoverflow.com
🌐
AskPython
askpython.com › python › built-in-methods › python-bin-function
What is Python bin() function? - AskPython
August 6, 2022 - Python numpy.binary_repr() function is used to convert the data values of an array to the binary form in an element wise fashion in NumPy.
🌐
SciPython
scipython.com › blog › binning-a-2d-array-in-numpy
Binning a 2D array in NumPy
August 4, 2016 - In [x]: arr.reshape(2, 2, 3, 2).mean(-1).mean(1) Out[x]: array([[ 3.5, 5.5, 7.5], [ 15.5, 17.5, 19.5]]) This is the $2\times 3$ binned array that we wanted.
🌐
Python Course
python-course.eu › numerical-programming › binning-in-python-and-pandas.php
34. Binning in Python and Pandas | Numerical Programming
February 3, 2025 - "cut" is the name of the Pandas function, which is needed to bin values into bins. "cut" takes many parameters but the most important ones are "x" for the actual values und "bins", defining the IntervalIndex. "x" can be any 1-dimensional array-like structure, e.g.
🌐
Statology
statology.org › home › how to bin variables in python using numpy.digitize()
How to Bin Variables in Python Using numpy.digitize()
May 24, 2022 - import numpy as np #create data data = [2, 4, 4, 7, 12, 14, 20, 22, 24, 31, 34] #place values into bins bin_data = np.digitize(data, bins=[10, 20]) #view binned data bin_data array([0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2]) #count frequency of each bin np.bincount(bin_data) array([4, 2, 5]) ... Bin “0” contains 4 data values. Bin “1” contains 2 data values. Bin “2” contains 5 data values. Find more Python tutorials here.
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.stats.binned_statistic.html
binned_statistic — SciPy v1.18.0 Manual
‘max’ : compute the maximum of values for point within each bin. Empty bins will be represented by NaN. function : a user-defined function which takes a 1D array of values, and outputs a single numerical statistic. This function will be called on the values in each bin.
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.histogram.html
numpy.histogram — NumPy v2.5 Manual
Input data. The histogram is computed over the flattened array. ... If bins is an int, it defines the number of equal-width bins in the given range (10, by default).
🌐
Real Python
realpython.com › lessons › binary-arrays
Binary Arrays (Video) – Real Python
In this lesson, I’ll be showing you how to deal with binary data using binary arrays. Python provides two array types for dealing with binary data, the first of which is called bytes. A…
Published: February 23, 2021
🌐
Educative
educative.io › answers › what-is-numpybincount-in-python
What is numpy.bincount() in Python?
The bincount() method of the NumPy module is used to find the frequency of each element in a NumPy array of positive integers. The element’s index in the frequency array or bin is stored as the element’s count.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert an integer to a binary array in python
5 Best Ways to Convert an Integer to a Binary Array in Python - Be on the Right Side of Change
February 18, 2024 - The recursive function int_to_binary_array calls itself with the quotient of the number divided by 2, until the base case (number less than 2) is reached. The remainder is then collected in a list which represents the binary array. A compact and efficient way uses the numpy library functions binary_repr(), which returns a binary string, and fromstring() which can convert this string into an array.
🌐
Real Python
realpython.com › ref › builtin-functions › bin
bin() | Python’s Built-in Functions – Real Python
Returns a string representing the binary equivalent of the given integer prefixed with “0b”.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › binning-data-in-python-with-scipy-numpy
Binning Data In Python With Scipy & Numpy - GeeksforGeeks
July 23, 2025 - The values in the array correspond to the 75th percentile of the data within the respective bins. Some bins may not have enough data points to calculate the 75th percentile, resulting in nan (not a number) values. For example, the second bin has a nan value because there might not be enough data in that bin to compute the 75th percentile. In conclusion, these diverse approaches to data binning in Python showcase the versatility of libraries like numpy, scipy, and pandas.
🌐
Reddit
reddit.com › r/learnpython › fill array with binary numbers
r/learnpython on Reddit: Fill array with binary numbers
June 12, 2019 -

Hello there. I am trying to learn python for a day. I need to create something like this:

5~ inputs

array = {}

fill array with binary 0(00000) to 31(11111) and execute some other function (not relevant here). So I need that array to be for i=0 -> array = {0,0,0,0,0}, i=1 -> array = {0,0,0,0,1}, i=2 -> array = {0,0,0,1,0} and so on, I think it can be also reversed, like i=1 -> array = {1,0,0,0,0} etc.

I managed to do a for loop to get my binary numbers 00000, 00001 etc, how can I split these numbers and put them into each array position?

🌐
GeeksforGeeks
geeksforgeeks.org › numpy-bincount-python
numpy.bincount() in Python - GeeksforGeeks
November 17, 2020 - In an array of +ve integers, the numpy.bincount() method counts the occurrence of each element. Each bin value is the occurrence of its index. One can also set the bin size accordingly.
Top answer
1 of 2
1

I cant find the original author of a different SO post where I got this from using Pandas but maybe try something like this below that I thru together really fast for an idea to try. The data frame is just numpy random range to generate the fake data in the ranges you are looking for.

import pandas as pd
import numpy as np

#create bins & categories for data ranges
cats = ['4100000_4155303',
        '4155304_4210608',
        '4210608_4321215',
        '4321216_4542431',
        '4542432_4984864',
        '4984865_5327532',
        '5327533_5670200',
        '5670201_5746216',
        '5746217_5873108',
        '5873109_6000000']

bins = [0,
        4100000,
        4210608,
        4321215,
        4542431,
        4984864,
        5327532,
        5670200,
        5746216,
        5873108,
        6000000]


def binn(df):
    df = (df.groupby([df.index, pd.cut(df['A'], bins, labels=cats)])
                .size()
                .unstack(fill_value=0)
                .reindex(columns=cats, fill_value=0))
    return df


rng = np.random.default_rng()
df = pd.DataFrame(rng.integers(4155304, 6000000, size=(1000, 1)), columns=list('A'))

dfBinned = binn(df)

print('All data binned in column A of the df')
print(dfBinned.sum(axis = 0))

This prints:

All data binned in column A of the df
A
4100000_4155303      0
4155304_4210608     35
4210608_4321215     42
4321216_4542431    130
4542432_4984864    239
4984865_5327532    174
5327533_5670200    205
5670201_5746216     37
5746217_5873108     63
5873109_6000000     75
dtype: int64
2 of 2
0

Simply use the numpy.arange method:

bins = np.arange(4100000, 6000000, 55304)
bins

Output

array([4100000, 4155304, 4210608, 4265912, 4321216, 4376520, 4431824,
       4487128, 4542432, 4597736, 4653040, 4708344, 4763648, 4818952,
       4874256, 4929560, 4984864, 5040168, 5095472, 5150776, 5206080,
       5261384, 5316688, 5371992, 5427296, 5482600, 5537904, 5593208,
       5648512, 5703816, 5759120, 5814424, 5869728, 5925032, 5980336])

Cheers

🌐
Saturn Cloud
saturncloud.io › blog › a-comprehensive-guide-to-binning-an-array-with-numpy-for-data-scientists
Saturn Cloud | Saturn Cloud | The Control Plane for GPU Clouds
July 23, 2023 - Saturn Cloud is the white-labeled control plane for GPU clouds: multi-tenant isolation, day-2 support, and integrated billing, running in your cloud under your brand.
🌐
Medium
medium.com › @heyamit10 › understanding-binning-in-numpy-02c169788d56
Understanding Binning in NumPy
March 6, 2025 - NumPy provides two primary ways to perform binning: ... Let’s explore both step by step. ... import numpy as np # Test scores scores = np.array([45, 67, 89, 34, 77, 90, 59, 82]) # Define bin edges bins = np.array([50, 70, 85, 100]) # Ranges: 0-50, 51-70, 71-85, 86-100 # Assign each score to a bin bin_indices = np.digitize(scores, bins) print("Scores:", scores) print("Assigned Bins:", bin_indices)