-2

I have a dictionary A:

A =  {"('All', 'Delhi', 'Mumbai')": {}, "('Container', 'Delhi', 'Mumbai')": {}, 
      "('Open', 'Delhi', 'Mumbai')": {12: [12, 22, 25], 7: [9, 5]},
      "('Open', 'Doon', 'Gurgaon')": {10: [1, 2, 24], 8: [4], 9: [28, 8], 7:[21]}}

I want to remove all the empty dictionaries, How can I do that ?

Rahul Sharma
  • 2,187
  • 6
  • 31
  • 76

2 Answers2

1

Use a comprehension:

>>> {k:v for k,v  in A.items() if v}
{"('Open', 'Delhi', 'Mumbai')": {12: [12, 22, 25], 7: [9, 5]}, "('Open', 'Doon', 'Gurgaon')": {10: [1, 2, 24], 8: [4], 9: [28, 8], 7: [21]}}
Netwave
  • 40,134
  • 6
  • 50
  • 93
0

You might use a comprehension here:

new_dict = {k: v for k, v in A.items() if v}
print(new_dict)

See a demo on ideone.com.

Jan
  • 42,290
  • 8
  • 54
  • 79