min

Manage empty list/invalid input when finding max/min value of list (Python)

Manage empty list/invalid input when finding max/min value of list (Python) Question: I’m finding max value and min value of a list by using max(list) and min(list) in Python. However, I wonder how to manage empty lists. For example if the list is an empty list [], the program raises ‘ValueError: min() arg is an …

Total answers: 3

Tuple pairs, finding minimum using python

Tuple pairs, finding minimum using python Question: I want to find the minimum of a list of tuples sorting by a given column. I have some data arranged as a list of 2-tuples for example. data = [ (1, 7.57), (2, 2.1), (3, 1.2), (4, 2.1), (5, 0.01), (6, 0.5), (7, 0.2), (8, 0.6)] How …

Total answers: 4

numpy.max or max ? Which one is faster?

numpy.max or max ? Which one is faster? Question: In python, which one is faster ? numpy.max(), numpy.min() or max(), min() My list/array length varies from 2 to 600. Which one should I use to save some run time ? Asked By: Froyo || Source Answers: It’s probably best if you use something like the …

Total answers: 4

How to find min/max values from rows and columns in Python?

How to find min/max values from rows and columns in Python? Question: I was wondering how can I find minimum and maximum values from a dataset, which is basically a text file. It has 50 rows, 50 columns. I know I can set up a control loop (for loop to be specific) to have it …

Total answers: 4

Finding minimum value in an array of dicts

Finding minimum value in an array of dicts Question: I have an array like the following: people = [{‘node’: ‘john’, ‘dist’: 3}, {‘node’: ‘mary’, ‘dist’: 5}, {‘node’: ‘alex’, ‘dist’: 4}] I want to compute the minimum of all the ‘dist’ keys. For instance, in the above example, the answer would be 3. I wrote the …

Total answers: 4

How to limit a number to be within a specified range? (Python)

How to limit a number to be within a specified range? (Python) Question: I want to limit a number to be within a certain range. Currently, I am doing the following: minN = 1 maxN = 10 n = something() #some return value from a function n = max(minN, n) n = min(maxN, n) This …

Total answers: 5

Remove Max and Min values from python list of integers

Remove Max and Min values from python list of integers Question: I am not completely green to Python, but I am interested in learning/keeping good practices while I develop my skills. I want to remove the high and low values from a list of numbers, which I know how to do, but am curious if …

Total answers: 3

How to find the min/max value of a common key in a list of dicts?

How to find the min/max value of a common key in a list of dicts? Question: I have a list of dictionaries like so: [{‘price’: 99, ‘barcode’: ‘2342355’}, {‘price’: 88, ‘barcode’: ‘2345566’}] I want to find the min() and max() prices. Now, I can sort this easily enough using a key with a lambda expression …

Total answers: 5

Get the key corresponding to the minimum value within a dictionary

Get the key corresponding to the minimum value within a dictionary Question: If I have a Python dictionary, how do I get the key to the entry which contains the minimum value? I was thinking about something to do with the min() function… Given the input: {320:1, 321:0, 322:3} It would return 321. Asked By: …

Total answers: 16

Getting the index of the returned max or min item using max()/min() on a list

Getting the index of the returned max or min item using max()/min() on a list Question: I’m using Python’s max and min functions on lists for a minimax algorithm, and I need the index of the value returned by max() or min(). In other words, I need to know which move produced the max (at …

Total answers: 23