1

What is the typecode 'x' of a dictionary?

dict_array = array.array('x', [dict1, dict2, dict3])

I don't know what to put at 'x'. Is there another way it can be done or is it not possible? I don't want a list of dicts, I want an array of them.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 3
    https://docs.python.org/3/library/array.html – "`array` — Efficient arrays of ***numeric*** values" – meowgoesthedog Feb 14 '19 at 16:36
  • Or maybe using a dictionary of dictionaries? I often find that useful and easier to organize. – Chicrala Feb 14 '19 at 16:36
  • If you really don't want a list, your best bet would be a dictionary of dictionaries with numeric keys. – Mikael Brenner Feb 14 '19 at 16:37
  • 1
    Pray tell, _why_ do you need to use an array? a list would be the correct data structure to use. Not to mention that anyway arrays can't contain dictionaries, only numeric values! – Óscar López Feb 14 '19 at 17:09

2 Answers2

1

A hack (which only works with CPython) would be to store a pointer to each dictionary in the array:

import array
import _ctypes


def di(obj_id):
    """ Reverse of id() function. """
    # from https://stackoverflow.com/a/15012814/355230
    return _ctypes.PyObj_FromPtr(obj_id)


dict1 = {'key': '1'}
dict2 = {'key': '2'}
dict3 = {'key': '3'}

dict_array = array.array('q', map(id, [dict1, dict2, dict3]))

for i, ptr in enumerate(dict_array):
    print('dict_array[{}]: <0x{:08x}> {!r}'.format(i, ptr, di(ptr)))

Output:

dict_array[0]: <0x00946630> {'key': '1'}
dict_array[1]: <0x00946690> {'key': '2'}
dict_array[2]: <0x00d80660> {'key': '3'}

However @tobias_k suggested a simpler and much better (IMO) approach that uses integer dictionary keys instead of memory pointers.

Here's an example of doing that:

import array

dicts = {
    0: {'key': '1'},
    1: {'key': '2'},
    2: {'key': '3'},
}

dict_array = array.array('L', dicts.keys())

for i, key in enumerate(dict_array):
    print('dict_array[{}]: {!r}'.format(i, dicts[key]))

Output:

dict_array[0]: {'key': '1'}
dict_array[1]: {'key': '2'}
dict_array[2]: {'key': '3'}
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    Depending on what OP wants to do with that array you could do the same putting the dictionaries in another dict or list and storing the index to that in the array. – tobias_k Feb 14 '19 at 17:07
0

As the other comments have indicated, a dictionary of dictionaries would be your best bet. You would first define your individual dictionaries like the following:

dict1 = {1 : 'first value', 2 : 'second value'}
dict2 = {1 : 'first value', 2 : 'second value'}
dict3 = {1 : 'first value', 2 : 'second value'}

Then to define an array in which the keys are the indexes:

dict_of_dicts = {1 : dict1, 2 : dict2, 3 : dict3}

Note: The indexes can match array notation, starting from 0, if you choose.

Then, you may access dictionary elements as such (in example of printing every element):

#This will neatly print out each dictionary, and its' elements, inserting a newline after each element has been printed.
for key, value in dict_of_dicts.items():
    print('{} = {'.format(key), end='')
    for i in value:
        print(i, end='')
    print('}')

This is probably your best option if you do not want a list. However, if for some reason it really does need to be an array of dictionaries, then visit the link @meowgoesthedog posted.

JCoder96
  • 178
  • 13