Search code examples
pythonlistperformancedictionarymemory-efficient

Efficient way to convert a dict into a flat data structure (list or tuple)


I implemented a breadth-first search for a board game. Here I use a dict to reflect duplicate board configurations on each level. Currently, the overall search takes nearly all the RAM I have (16GB) for one start configuration. I plan to integrate intersection checks for different start configurations. So I need read access to the configurations I found, and the dict for one level doesn't change if the level is finished.

That's why I plan to convert the dict into a flat data structure (list or tuple) with keys at [2n] position and values at [2n+1] position before I evaluate the next level.

The problem is to find a fast conversion from {1: 2, 3: 4} to [1, 2, 3, 4] for dictwith more than 10**8 items.

I found sum(dict.items(), ()) from a comment by Natim to another question, which worked, but which is too slow (it seemingly stops working for dicts with more than 10**6 items).


Solution

  • Use itertools function chain and classmethod alternative constructor from_iterable:

    >>> from itertools import chain
    >>> list(chain.from_iterable(dct.items()))
    [1, 2, 3, 4]
    >>> 
    

    Or with operator.iconcat and functools.reduce:

    >>> import operator, functools
    >>> functools.reduce(operator.iconcat, dct.items(), [])
    [1, 2, 3, 4]
    >>>