What would be a way to update this for loop to use map() instead, and is possible to shorten the if code too? I will write some examples:
def function(self, box):
if len(box) == 0:
for objID in list(self.dict.keys()):
self.dict[objID] += 1
if self.dict[objID] > self.maxF:
self.deleteObject(objID)
return self.object
This is just a part of the code but I would like to learn how to shorten it and get it to work faster perhaps, but the main point is I would like to learn to write it better.
for objID in list(self.dict.keys()):tofor objID in self.dict.keys():Otherwise this code is fine. One-liners don't necessarily run faster than full loops, and complicated one-liners are hard to understand.listbecause I suspect that self.deleteObject() is deleting the specified object from the dictionary. You can't delete an object from a dictionary at the same time you're iterating over it. Copying the keys is one solution.map.mapis made for) so don't try to use map here