I think you want a dictionary where each key is the id and the value is your object.

Try the following. data is your array of data.

new = {}
for item in data:
    item_id = item['test']['id']
    new[item_id] = item

You could also use a dictionary comprehension.

new = {item['test']['id']: item for item in data}
Answer from Ozzy Walsh on Stack Overflow
Top answer
1 of 3
11

You may be looking for pandas, which provides handy datatypes that wrap numpy arrays, allowing you do access rows and columns by name instead of just by number.

2 of 3
2

I dislike giving ready made answers - but I think it would take much more time to explain it in English -

The basic idea to fetch objects the way numpy does is to customize the __getitem__ method - comma separated values are presented to the method as tuples - you them just use the values in the tuple as indexes to your nested dictionaries in sequence.

Beyond that, Python made easy to create fully functional dict equivalentes with the collections.abc classes: if you implement a minimal set of methods when inhetiring from collections[.abc].MutableMapping, all dictionary behavior is emulated - (__getitem__, __setitem__, __delitem__, __iter__, __len__) - Then, it is just a matter of proper iterating through the key components, and create new, empty, regular dictionaries to store the needed values.

try:
    from collections import MutableMapping
except ImportError:
    # Python3 compatible import
    from collections.abc import MutableMapping

class NestedDict(MutableMapping):
    def __init__(self, *args, **kw):
        self.data = dict(*args, **kw)

    def get_last_key_levels(self, key, create=False):
        if not isinstance(key, tuple):
            key = (key,)
        current_data = self.data
        for subkey in key:
            previous = current_data
            current_data = current_data[subkey] if not create else current_data.setdefault(subkey, {})
        return previous, current_data, subkey

    def __getitem__(self, key):
        previous, current_data, lastkey = self.get_last_key_levels(key)
        return current_data

    def __setitem__(self, key, value):
        previous, current_data, lastkey = self.get_last_key_levels(key, True)
        previous[lastkey] = value

    def __delitem__(self, key):
        previous, current_data, lastkey = self.get_last_key_levels(key)
        del previous[lastkey]

    def __iter__(self):
        return iter(self.data)

    def __len__(self):
        return len(self.data)

    def __repr__(self):
        return "NestedDict({})".format(repr(self.data))

And you are set to go:

>>> from nesteddict import NestedDict
>>> x = NestedDict(a={})
NestedDict({'a': {}})
>>> x["a", "b"] = 10
>>> x
NestedDict({'a': {'b': 10}})
>>> x["a", "c", "e"]  = 25
>>> x
NestedDict({'a': {'c': {'e': 25}, 'b': 10}})
>>> x["a", "c", "e"] 
25
>>> 

Please note that this is a high-level implementation, which will just work, but you will have nowhere near the optimization level you get on NumPy with this - to the contrary. If you will need to perform fast data operations in these objects, you maybe could check "cython" - or resort to your idea of transposing the dict keys to nuemric keys and use NumPy (that idea could still pick some ideas from this answer)

Discussions

arrays - store value with multidimensional key pair in python - Stack Overflow
is there a way to store a value (e.g. in a numpy array) with a multidimensional key pair? The code below tries to store a reward value with two numpy arrays as the key pair with shape (1,25) and (... More on stackoverflow.com
🌐 stackoverflow.com
Multidimensional arrays?
If you just use normal lists, you can store whatever you like. data = [ [1, 2, 3], [4], [5, 6, 7, 8, 9, 10], ] And you can refer to them exactly as you say: data[2][3] which will give you 8. More on reddit.com
🌐 r/learnpython
10
1
September 15, 2023
Multidimensional array in Python - Stack Overflow
I have a little Java problem I want to translate to Python. Therefor I need a multidimensional array. More on stackoverflow.com
🌐 stackoverflow.com
How to get key filtered by its value in multidimensional array Python - Stack Overflow
I got this data employee_detail_list = { 'John Doe': { 'name': 'EMP-0001', 'first_name': 'John', 'last_name': 'Doe', 'full_name': 'John Doe', ' More on stackoverflow.com
🌐 stackoverflow.com
🌐
DaniWeb
daniweb.com › programming › software-development › code › 275835 › multidimensional-array-using-a-dictionary-python
Multidimensional Array using a Dictionary ... | DaniWeb
April 14, 2010 - # use Python3's dictionary comprehension to mimic # a 2 dimensional array with a dictionary # with (row, column) index tuples as keys # tested with Python 3.1.2 by vegaseat def print_sortkey(d): """ print dictionary d with keys sorted """ for key in sorted(d): print( "%s --> %s" % (key, d[key]) ) # create a 3 x 4 2D-array using a dictionary with index tuples # as keys, initialize with zero d_arr = {(x,y):0 for x in range(3) for y in range(4)} print( d_arr ) print( '-'*20 ) """my result (tuples are in hash order) --> {(0, 1): 0, (1, 2): 0, (0, 0): 0, (2, 1): 0, (0, 2): 0, ...} """ # change some values, basically assign to index d_arr[2, 1] = 5 # or ...
🌐
Stack Overflow
stackoverflow.com › questions › 50487164 › store-value-with-multidimensional-key-pair-in-python
arrays - store value with multidimensional key pair in python - Stack Overflow
You could have table_r be a dict of dicts, with tuple(s[0]) being the first key and tuple(a) the second key: Copynum_episodes=500 # this is the table that will hold our summated rewards for # each action in each state r_table = {} for g in range(num_episodes): s = np.array(state.sample(), dtype=np.int) done = False count = 0 while not done: s_key = tuple(s[0]) if sum(r_table.setdefault(s_key, {}).values()) == 0: # make a random selection of actions EUR_elec_sell = 0.050 EUR_elec_buy = 0.100 EUR_gas = 0.030 rranges = ((0, 1250),(0, 2000),(0, 3000)) res0 = brute(reward, rranges, finish=None) res
🌐
Reddit
reddit.com › r/learnpython › multidimensional arrays?
r/learnpython on Reddit: Multidimensional arrays?
September 15, 2023 -

Hello!

I have 5 sets, each set contains a number of elements of variable length (eg. set 1 has 100 elements, set 2 has 150 and so on)

Is it possible to store these sets in a single structure?

For example, if I want to print the 35th elements of the 3rd set, I could call it simply by saying something like MyContainer[setN][elementN]

I was trying to use a 2D array but I can't initialize its shape, since each set has a variable number of elements.

Can anybody point me to the right direction / best practice?

Thank you

🌐
CodeSignal
codesignal.com › learn › courses › multidimensional-arrays-and-their-traversal-in-python › lessons › exploring-the-dimensions-a-beginners-guide-to-multidimensional-arrays-in-python
Exploring the Dimensions: A Beginner's Guide to Multidimensional Arrays in Python | CodeSignal Learn
To construct a multidimensional or nested array in Python, we use lists inside lists. Here's an example of a 2-dimensional array: ... In this example, array is a 2-dimensional array, just like a 3-storey 'apartment building,' where every floor is an inner list.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AccessingDataAlongMultipleDimensions.html
Accessing Data Along Multiple Dimensions in an Array — Python Like You Mean It
Keeping track of the meaning of an array’s various dimensions can quickly become unwieldy when working with real datasets. xarray is a Python library that provides functionality comparable to NumPy, but allows users provide explicit labels for an array’s dimensions; that is, you can name each dimension.
Find elsewhere
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › multidimensional array in python
Multidimensional Array in Python | Creating a Multidimensional List
April 17, 2023 - Numpy is a pre-defined package in python used for performing powerful mathematical operations and support an N-dimensional array object. Numpy’s array class is known as “ndarray”, which is key to this framework. Objects from this class are referred to as a numpy array. The difference between Multidimensional ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
GeeksforGeeks
geeksforgeeks.org › python › multi-dimensional-lists-in-python
Multi-dimensional Lists in Python - GeeksforGeeks
October 30, 2025 - This approach retrieves each row from the outer list directly. It’s useful when you want to process or print entire rows one at a time without worrying about column indices.
🌐
NumPy
numpy.org › doc › stable › reference › arrays.ndarray.html
The N-dimensional array (ndarray) — NumPy v2.5 Manual
An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension. The type of items in the array is ...
🌐
Medium
soumenatta.medium.com › how-to-work-with-multidimensional-arrays-in-python-a-beginners-guide-bd437495c87
How to Work with Multidimensional Arrays in Python: A Beginner’s Guide | by Dr. Soumen Atta, Ph.D. | Medium
April 8, 2023 - Multidimensional arrays, also known as “nested arrays” or “arrays of arrays,” are an essential data structure in computer programming. In Python, multidimensional arrays can be implemented using lists, tuples, or numpy arrays.
🌐
AskPython
askpython.com › python › array › multidimensional-arrays
Multidimensional Arrays in Python: A Complete Guide - AskPython
February 27, 2023 - In this article, the creation and implementation of multidimensional arrays (2D, 3D as well as 4D arrays) have been covered along with examples in Python Programming language. To understand and implement multi-dimensional arrays in Python, the NumPy package is used.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to create a two dimensional array in python?
How To Create a Two Dimensional Array in Python? - Be on the Right Side of Change
June 11, 2022 - Finally, we print the array with the help of a for loop by simply displaying the keys of the dictionary which are storing the lists, i.e., the individual rows of the 2D array. The most convenient way of working with arrays in Python is to use Python’s Numpy library. Python has this amazing module named NumPy that can be used to create multidimensional arrays.
🌐
NumPy
numpy.org › devdocs › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.6.dev0 Manual
The NumPy library contains multidimensional array data structures, such as the homogeneous, N-dimensional ndarray, and a large library of functions that operate efficiently on these data structures. Learn more about NumPy at What is NumPy, and if you have comments or suggestions, please reach out! After installing NumPy, it may be imported into Python code like: ... This widespread convention allows access to NumPy features with a short, recognizable prefix (np.) while distinguishing NumPy features from others that have the same name.
🌐
freeCodeCamp
freecodecamp.org › news › multi-dimensional-arrays-in-python
Multi-Dimensional Arrays in Python – Matrices Explained with Examples
December 11, 2025 - You'll commonly use these types of arrays in fields such as mathematics, statistics, and computer science to represent and process structured data, such as images, videos, and scientific data. In Python, you can create multi-dimensional arrays using various libraries, such as NumPy, Pandas, and TensorFlow.
🌐
Quora
quora.com › Can-I-use-a-dictionary-like-a-two-dimensional-array
Can I use a dictionary like a two-dimensional array? - Quora
Answer (1 of 5): In Python, you absolutely can, and you don’t need to nest dictionaries either; just use a 2-tuple as the key (where the 2-tuple are the x,y index). I have a number of applications that do exactly that using a 2-tuple as a key into a dictionary to represent a grid in some form: ...