Finding the Maximum and Minimum in a Python set


In this article, the user will see how to calculate the maximum and minimum in a set using Python language. The main feature of the python set is that it contains no duplicates elements, store the elements in unordered form and is also difficult to alter the elements presented in the set. In this article, three different methods are illustrated that is Converting the set to a list, using built-in functions, and using the sorted() function. The ‘sorted()’ function retrieves a set’s element in a sorted way.

Approach 1: Finding the maximum and minimum in a Python set using converting the set to a list.

Code Explanation and Design Steps

Step 1: Define the set that contains multiple integer values.

Step 2: Convert a set into a list using the ‘list()’ function.

Step 3: Utilize the ‘min()’ and ‘max()’ functions to obtain the maximum value and minimum value.

Step 4: Display the result.

Code for maximum and minimum in a set using converting the set to a list.

Example

#initialize the set
my_set = {5, 2, 8, 1, 9, 2, 18}

# Convert set to a list
my_list = list(my_set)

# Find the maximum and minimum values
maximum = max(my_list)
minimum = min(my_list)
# Display maximum and minimum value
print("Maximum:", maximum)
print("Minimum:", minimum)

Output

Maximum: 18
Minimum: 1

In the above program, the set would be transformed into a list, and then use ‘max()’ and ‘min()’ functions to retrieve values that are minimum and maximum.

Note: While converting the set to a list, it discards any duplicates and also changes the ordering.

Approach 2: Finding the Maximum and Minimum in a Python Set using built-in functions.

Code Explanation and Design Steps

Step 1: Initialize the set that consists of integer values.

Step 2: The ‘min()’ and ‘max()’ functions would be used to get the values.

Step 3: Finally, display the result.

Code for maximum and minimum in a set using direct max() and min() function

Example

#define set named my_set
my_set = {5, 2, 8, 1, 9, 2, 18}

# Find the maximum and minimum values
maximum = max(my_set)
minimum = min(my_set)
#display the resulting value
print("Maximum:", maximum)
print("Minimum:", minimum)

Output

Maximum: 18
Minimum: 1

This is a direct approach to get the maximum and minimum in assets using the ‘max()’ and ‘min()’ functions. Both functions return the maximum and minimum values in the set respectively.

Approach 3: Finding the Maximum and Minimum in a Python Set using sorted() function.

Code Explanation and Design Steps

Step 1: Open Jupyter Notebook in the Anaconda prompt and start writing the code in its cell.

Step 2: Apply the ‘sorted()’ function to the set.

Step 3: Use the ‘min()’ and ‘max()’ functions to obtain the largest values and smallest values.

Step 4: Print the specified result.

Code for maximum and minimum in a set using the sorted() function

Example

#Creation of a set
my_set = {5, 2, 8, 1, 9, 2, 18}
# Sort a set 
sorted_list = sorted(my_set)
# Find the maximum and minimum values
min_value = sorted_list[0]
max_value = sorted_list[-1]
#display the maximum value
print("Maximum value:", max_value)
#dipslay the minimum value
print("Minimum value:", min_value)

Output

Maximum: 18
Minimum: 1

Conclusion

The three approaches are depicted in this article to obtain maximum and minimum values in the set. The first approach is to use to convert the set into a list and apply ‘max()’ and ‘min()’ functions to achieve both values from the set. In the second approach, ‘max()’ and ‘min()’ functions can be applied directly to the set. The last approach uses the ‘sorted()’ function for the same purpose. It has several applications in different domains.

Updated on: 28-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements