aldegalan
aldegalan

Reputation: 502

Print all the elements meeting a condition JSON

Having a json like this one:

json = {'example1': 0, 'example2': 16, 'example3': 12}

Is it possible to print only the ones who are greater than 10?

Upvotes: 0

Views: 41

Answers (1)

mhawke
mhawke

Reputation: 87094

Yes.

>>> json = {'example1': 0, 'example2': 16, 'example3': 12}
>>> print(*[k for k in json if json[k] > 10])
example2 example3

The idea here is to use a list comprehension that iterates over the keys of the dictionary and filters those for which the value is greater than 10.

The filtered items are then unpacked for printing by print().

Upvotes: 1

Related Questions