0

I need to build a dictionary from a list with the size of about 3 million items, right now I am using the code below but it is running for more than 12 hours and still not done. I was wondering whether there is a faster way?. This is the code I am using right now:

my_dict = {i:obj_id.count(i) for i in obj_id}
MRM
  • 1,099
  • 2
  • 12
  • 29

1 Answers1

0

The reason your code is running so slow is that it has time complexity of O(N) to find the count and then another O(N) time to populate the dictionary. The solution is to iterate over the list, add key obj and value 1 if there key did not exist before. If it did, just increment the value. This will cut some complexity because accessing and modifying a dictionary is 0(1) time.

my_dict = {}
for obj in obj_id{
    if obj in my_dict{
        my_dict[obj] += 1
    }
    else{
        my_dict[obj] = 1
    }
}
Jae Yang
  • 479
  • 3
  • 13