'Get Smallest Value In Array Of JSON Objects
I have a list that looks something like this,
[{'Device': 'A', 'CPU': 10.7, 'RAM': 32.5}, {'Device': 'B', 'CPU': 4.2, 'RAM': 32.4}, {'Device': 'C', 'CPU': 0.7, 'RAM': 32.5}, {'Device': 'D', 'CPU': 57.8, 'RAM': 38.5}]
Basically I need to get the smallest CPU value from the array and I do not know the best way to approach this. Thanks.
Solution 1:[1]
>>> devices = [{'Device': 'A', 'CPU': 10.7, 'RAM': 32.5}, {'Device': 'B', 'CPU': 4.2, 'RAM': 32.4}, {'Device': 'C', 'CPU': 0.7, 'RAM': 32.5}, {'Device': 'D', 'CPU': 57.8, 'RAM': 38.5}]
>>> min(devices, key = lambda x: x['CPU'])
{'Device': 'C', 'CPU': 0.7, 'RAM': 32.5}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrey Lukyanenko |
