Python | Min/Max value in float string list
Last Updated :
08 Apr, 2023
Sometimes, while working with a Python list, we can have a problem in which we need to find min/max value in the list. But sometimes, we don't have a natural number but a floating-point number in string format. This problem can occur while working with data, both in web development and Data Science domain. Let's discuss a way in which this problem can be solved.
Method 1: Using min()/max() + float()
This problem can be solved using the min or max function in which we first convert the strings into float and then pass this logic in functions in respective min/max function.
Python3
# Python3 code to demonstrate working of
# Min / Max value in float string list
# using min()/max() + float() + generator
# initialize lists
test_list = ['4.5', '7.8', '9.8', '10.3']
# printing original list
print("The original list is : " + str(test_list))
# Min / Max value in float string list
# using min()/max() +float + lambda function
res_min = min(test_list,key=lambda x:float(x))
res_max = max(test_list,key=lambda x:float(x))
# printing result
print("The min value of list : " + str(res_min))
print("The max value of list : " + str(res_max))
OutputThe original list is : ['4.5', '7.8', '9.8', '10.3']
The min value of list : 4.5
The max value of list : 10.3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using the built-in reduce() function along with the lambda function to find the minimum and maximum values.
Using the built-in reduce() function along with the lambda function is one approach to finding the minimum and maximum values in a list of float strings. The reduce() function applies a given function to the elements of an iterable, cumulatively reducing them to a single value. By passing in a lambda function that compares the current minimum or maximum value to the next element in the list, the reduce() function can be used to find the minimum or maximum value.
Python3
from functools import reduce
test_list = ['4.5', '7.8', '9.8', '10.3']
# Find minimum value
res_min = reduce(lambda x, y: x if float(x) < float(y) else y, test_list)
# Find maximum value
res_max = reduce(lambda x, y: x if float(x) > float(y) else y, test_list)
# printing result
print("The min value of list : " + str(res_min))
print("The max value of list : " + str(res_max))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe min value of list : 4.5
The max value of list : 10.3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3: Use a for loop and convert each element to float before comparing them.
Step-by-step approach:
- Initialize variables for minimum and maximum values to None.
- Iterate through the list using a for loop.
- Convert each element to float using the float() function.
- Compare the float value to the current minimum value. If the value is less than the current minimum value or if the minimum value is None, update the minimum value.
- Compare the float value to the current maximum value. If the value is greater than the current maximum value or if the maximum value is None, update the maximum value.
- After iterating through the entire list, the minimum and maximum values will be stored in their respective variables.
- Print the minimum and maximum values.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of
# Min / Max value in float string list
# using for loop and float()
# initialize list
test_list = ['4.5', '7.8', '9.8', '10.3']
# printing original list
print("The original list is : " + str(test_list))
# initialize variables for minimum and maximum values
min_val = None
max_val = None
# iterate through the list and find minimum and maximum values
for elem in test_list:
float_elem = float(elem)
if min_val is None or float_elem < min_val:
min_val = float_elem
if max_val is None or float_elem > max_val:
max_val = float_elem
# printing result
print("The min value of list : " + str(min_val))
print("The max value of list : " + str(max_val))
OutputThe original list is : ['4.5', '7.8', '9.8', '10.3']
The min value of list : 4.5
The max value of list : 10.3
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1), as we only need to store two variables for the minimum and maximum values.
Method 5: Using numpy library
step-by-step approach :
- Import the numpy library using the statement import numpy as np.
- Define the input list test_list with string elements as ['4.5', '7.8', '9.8', '10.3'].
- Convert the input list to a numpy array of float elements using the np.array() method and assign it to a variable float_arr. The dtype parameter is set to float to ensure that the elements are of float data type.
- Use the np.min() function to get the minimum value from the float_arr array and assign it to a variable min_val.
- Use the np.max() function to get the maximum value from the float_arr array and assign it to a variable max_val.
- Print the result using the print() function. The minimum value and maximum value are printed using the variables min_val and max_val respectively.
Python3
# Python3 code to demonstrate working of
# Min / Max value in float string list
# using numpy
import numpy as np
# initialize list
test_list = ['4.5', '7.8', '9.8', '10.3']
# convert the list of string elements to a numpy array of float elements
float_arr = np.array(test_list, dtype=float)
# get the minimum and maximum values from the array
min_val = np.min(float_arr)
max_val = np.max(float_arr)
# printing result
print("The min value of list : " + str(min_val))
print("The max value of list : " + str(max_val))
OUTPUT:
The min value of list : 4.5
The max value of list : 10.3
The time complexity of this method is O(n) to convert the string list to a numpy array, and O(1) to get the minimum and maximum values using numpy functions. So the overall time complexity is O(n).
Auxiliary space: This method requires extra space to store the numpy array, which has a space complexity of O(n).
Similar Reads
Python - Find maximum value in each sublist Finding the maximum value in each sublist involves iterating through a list of lists and identifying the largest element within each sublist. This operation is useful for analyzing data sets, and extracting the most significant values from grouped information. In this article, we will explore method
2 min read
Python | Max/Min value in Nth Column in Matrix Sometimes, while working with Python Matrix, we may have a problem in which we require to find the minimum and maximum value of a particular column. This can have a possible application in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed
7 min read
Use of min() and max() in Python Prerequisite: min() max() in Python Let's see some interesting facts about min() and max() function. These functions are used to compute the maximum and minimum of the values as passed in its argument. or it gives the lexicographically largest value and lexicographically smallest value respectively,
2 min read
Check If Value Is Int or Float in Python In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to check if a value is an integer
4 min read
Cannot Convert String To Float in Python Python, a versatile and powerful programming language, is widely used for data manipulation and analysis. However, developers often encounter challenges, one of which is the "Cannot Convert String To Float" error. This error occurs when attempting to convert a string to a float, but the string's con
3 min read
Convert String to Float in Python The goal of converting a string to a float in Python is to ensure that numeric text, such as "33.28", can be used in mathematical operations. For example, if a variable contains the value "33.28", we may want to convert it to a float to perform operations like addition or division. Let's explore dif
2 min read
max() and min() in Python This article brings you a very interesting and lesser-known function of Python, namely max() and min(). Now when compared to their C++ counterpart, which only allows two arguments, that too strictly being float, int or char, these functions are not only limited to 2 elements, but can hold many eleme
3 min read
Float type and its methods in python A float (floating-point number) is a data type used to represent real numbers with a fractional component. It is commonly used to store decimal values and perform mathematical calculations that require precision.Characteristics of Float TypeFloats support decimal and exponential notation.They occupy
5 min read
Python Regex to extract maximum numeric value from a string When working with strings containing both text and numbers, itâs common to extract the largest numeric value embedded within the text, Pythonâs re module provides an efficient and concise way to achieve this. In this article, we will explore various methods for thisUsing re.findall and maxThis is th
2 min read
Convert String with Comma To Float in Python When working with data in Python, it's not uncommon to encounter numeric values formatted with a mix of commas and dots as separators. Converting such strings to float is a common task, and Python offers several simple methods to achieve this. In this article, we will explore five generally used met
3 min read