1

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.

7
  • 1
    You can change for objID in list(self.dict.keys()): to for 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. Commented Oct 18, 2020 at 20:50
  • 1
    @PranavHosangadi. They need to call list because 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. Commented Oct 18, 2020 at 20:56
  • And to original poster. I agree with @PranavHosangadi. Your code is nicely laid out and easy to understand. There is nothing to gain here by using map. Commented Oct 18, 2020 at 20:57
  • 1
    You don't transform each object in a list of object (what map is made for) so don't try to use map here Commented Oct 18, 2020 at 20:58
  • Thank you all for the answer, yes @FrankYellin it is deleting object from dictionary so list have to be used. Commented Oct 18, 2020 at 20:58

2 Answers 2

3

It's hard to say whether this will work without seeing your deleteObject function, but in general if I were going to modify a regular old dictionary like this I'd use a simple comprehension:

self.dict = {k: v+1 for k, v in self.dict.items() if v <= self.maxF}

(edit per comment) if there's meant to be an invariant where self.object and self.dict must have matching keys, then maybe I'd follow it up with:

self.object = {k: v for k, v in self.object.items() if k in self.dict}

However, if self.dict and self.object must have matching keys, it's a very strong clue that they should not be two different dictionaries in the first place -- instead there should be a single dictionary with compound values (objects, tuples, whatever).

Sign up to request clarification or add additional context in comments.

1 Comment

def deleteObject(objID): del self.object[objID] del self.dict[objID]
-1

can be approched with MAP as:

map(lambda objID: self.dict[objID] < self.maxF ? self.dict[objID] +=1 : self.deleteObject(objID), self.dict.keys() )

1 Comment

This is not python, python uses ... if condition else .... And jsut write a regular function definition is you are going to assign it to a variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.