0
Input :
t1_dict = {"k1":{"api1":200,"api2":500,"api3":200},"k2":{"api1":500,"api2":200,"api3":500}}

Output:
t2_dict = {'k2': {'api'1: 500,'api3':500}, 'k1': {'api2': 500}}

Need to extract whose values not equal 200 to another dictionary. I could able to do it, as below.

t2_dict = {}
   for k1,v1 in api_status.items():
      t2_dict[k1] = {}
      for k2, v2 in v1.items():
         if v2 != 200:
            t2_dict[k1][k2] = v2

Can it be done in better way ? Any one-liner or other best solution ? Any optimisation ?

StackGuru
  • 471
  • 1
  • 9
  • 25
  • I am not sure who and why downvoted this question. Is there any better and efficient approach ? – StackGuru Jul 08 '20 at 00:04
  • 1
    I downvoted your question because it doesn't show any research effort. This question was asked several times (dict of dicts dictionary comphrension filter....). You can delete or improve your question to get back your reputation. – cronoik Jul 08 '20 at 00:07
  • @cronoik : Thank you. That's exactly what I needed. Was not aware of dictionary one-liner for multi-level dictionary. – StackGuru Jul 08 '20 at 00:14

1 Answers1

1

Alternative one-liner (after the setup):

#Value to exclude
Value=200

t1_dict={"k1":{"api1":200,"api2":500,"api3":200},"k2":{"api1":500,"api2":200,"api3":500}}

t2_dict = {j:{k:v for k,v in t1_dict[j].items() if v != Value} for j in t1_dict}

print(t2_dict)

Credit to the top rated solution found here for the root of the idea, I simply nested it inside another loop: Remove a dictionary key that has a certain value

Hope this helps!