In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

+2 votes
[{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]

I want to find out the minimum and maximum. I know I can sort this easily using a basic lambda function using the key,
but I know python must have a direct function to pull this off. can someone help me out?
Apr 10, 2018 in Python by ariaholic
• 7,340 points
53 views

2 answers to this question.

+2 votes
Best answer
There are several options. Here is a straight-forward one:

seq = [x['the_key'] for x in dict_list]
min(seq)
max(seq)
[Edit]

If you only wanted to iterate through the list once, you could try this

import sys

lo,hi = sys.maxint,-sys.maxint-1
for x in (item['the_key'] for item in dict_list):
    lo,hi = min(x,lo),max(x,hi)
answered Apr 10, 2018 by charlie_brown
• 7,720 points

selected Oct 12, 2018 by Omkar
0 votes

If you only wanted to iterate through the list once, you could try this (assuming the values could be represented as ints):

import sys

lo,hi = sys.maxint,-sys.maxint-1
for x in (item['the_key'] for item in dict_list):
    lo,hi = min(x,lo),max(x,hi)
answered Oct 12, 2018 by findingbugs
• 4,750 points

Related Questions In Python

0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
620 views
0 votes
1 answer

How can I find the square of a number in python?

You can use the exponentiation operator or ...READ MORE

answered May 20 in Python by Mohammad
• 3,060 points
23 views
0 votes
2 answers

In List of Dicts, find min() value of a common Dict field

lst = [{'price': 99, 'barcode': '2342355'}, {'price': ...READ MORE

answered Aug 31, 2018 in Python by Omkar
• 67,290 points
80 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 175 views
0 votes
1 answer

How can I get the count of a list in Python?

len() >>> mylist=[] >>> print len(mylist) 0 READ MORE

answered Dec 12, 2018 in Python by charlie_brown
• 7,720 points
64 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
67 views