Python: the best way to add a dictionary with list values

I am creating a class, among other things, a dictionary with integer keys and list values. Adding values โ€‹โ€‹to this dictionary seems to be a real bottleneck, and I was wondering if there could be some way to speed up my code.

class myClass():

  def __init__(self):
    self.d = defaultdict(list)

  def addValue(self, index, value):
    self.d[index].append(value)

Is this really the best way to do this? I don't care about the order of the values, so maybe there is a more suitable data structure with faster additions. Again, "append" is apparently not the main problem, because if I just add to the empty list, the code is much faster. I assume loading a previously saved list takes most of the time?


I found out that the problem is not in the dict, but in the append list (although I stated differently in my original post, for which I apologize). This issue is due to a bug in the Python garbage collector, which is well explained in this other question . Disabling gc before adding all values, and then re-enabling, speeding up the process is very important!

+5
source share
3 answers

As a conclusion, I can say that my code in the original question is faster or faster than all the other sentences.

0
source

Compare this to this:

class myClass():

  def __init__(self):
    self.d = {}

  def addValue(self, index, value):
    self.d.setdefault(index, []).append(value)
+2
source

: " , ". , , , defaultdict, .

try this:

class myClass():

  def __init__(self):
    self.d = {}

  def addValue(self, index, value):
    try:
        self.d[index].append(value)
    except KeyError:
        self.d[index] = [value]

index , , KeyError .

?

+1

All Articles