I'm iterating through a list of over three million items and am assigning them integer values. For organization, I have made a dictionary whose keys are the integers and values are the list of items with that score. A priori, I do not know how many items will have a certain score, so I'm using the + operator to append onto the list as follows:
for e in xs:
myDict[val(e)] = myDict.get(val,[]) + [e]
My questions are:
- Is there a cleaner way of doing this?
- What's the time complexity of the + operation? Is it creating an entirely new list, copying the elements from the original list, and adding them in?
- What if I was adding an element to a set?