python key in dict.keys() performance for large dictionaries

后端 未结 5 1028
执念已碎
执念已碎 2020-12-09 20:22

I was wondering if you guys might be able to give me some advice in regards to making the performance of my code much better.

I have a set of for loops which look to

5条回答
  •  有刺的猬
    2020-12-09 20:58

    Step 1: we transform the code using the temp_list into a single expression (I assume temp_list isn't needed outside this code), by using addition instead of the append method. Also, we don't need to use dict.keys() explicitly, as others mentioned (and in fact it wastes a huge amount of time).

    for value in value_list:
       if value.key in dict:
          dict[value.key] = dict[value.key] + [value.val]
       else:
          dict[value.key] = [value.val]
    

    Step 2: Transform the assignments-to-the-same-location by using the conditional expression syntax.

    for value in value_list:
       dict[value.key] = dict[value.key] + [value.val] if value.key in dict else [value.val]
    

    Step 3: Appending or prepending an empty list has no effect on the value of a list, so we can insert that, and then factor out the common 'addition' of the value.

    for value in value_list:
       dict[value.key] = (dict[value.key] if value.key in dict else []) + [value.val]
    

    Step 4: Recognize that the dict has built-in functionality for providing a 'default' value when the key is absent:

    for value in value_list:
       dict[value.key] = dict.get(value.key, []) + [value.val]
    

    Step 5: Instead of getting a value, modifying it and setting it back, we can use .setdefault to give us the current contents (or set them up if not already there), and then switch back to using .append to modify the list:

    for value in value_list:
       dict.setdefault(value.key, []).append(value.val)
    

    (I mean... I could have just looked at it and thought for a bit and arrived at this, but seeing each step makes it clearer where we're going...)

提交回复
热议问题