Home > Net >  How to return lowest value from dictionary object with selected tags?
How to return lowest value from dictionary object with selected tags?

Time:01-25

How can I return lowest value from dictionary object with selected tags

findLowestQty(inputstring,'Qty')

InputString:

{
1235 : {'Id':1, 'Qty' : 25},
1236 : {'Id':2, 'Qty' : 35},
1237 : {'Id':3, 'Qty' : 15},
1238 : {'Id':4, 'Qty' : 45}
}

Output : ('1237', 15)

CodePudding user response:

I would use a list comprehension like so:

i = {
1235 : {'Id':1, 'Qty' : 25},
1236 : {'Id':2, 'Qty' : 35},
1237 : {'Id':3, 'Qty' : 15},
1238 : {'Id':4, 'Qty' : 45}
}
w = 'Qty'
b = min([d[w] for k, d in i.items()])
a = [j for j in i if i[j][w] == b]
print((str(a[0]), b))

Output:

('1237', 15)
  •  Tags:  
  • Related