0

I have a dictionary contains lists of values and a list:

dict={'first':45, 'second':30, 'third':56}
list= [30,45]

I want to compare the value in the dictionary with the list and a match to add to a new dictionary after that, remove from the old dict all the values that are in the new dict: I'm doing something like this:

    def get_sessions(self, talks):
      result_sessions = {}
      for k, v in self.sessions.items():
          for i in talks:
            if v == i:
                result_sessions[k] = v
      for k, v in result_sessions.items():
         del self.sessions[k]
     return result_sessions

Maybe you know a more elegant solution? any help?

Igor Pereverzev
  • 85
  • 1
  • 1
  • 7

1 Answers1

0

This is one approach.

Ex:

d ={'first':45, 'second':30, 'third':56}
lst = [30,45]

result_sessions = {k: v for k, v in d.items() if v in lst}
d = { k : d[k] for k in set(d) - set(result_sessions) }

print(result_sessions)
print(d)

Output:

{'second': 30, 'first': 45}
{'third': 56}
Rakesh
  • 81,458
  • 17
  • 76
  • 113