Open In App

Python | Maximum and Minimum in a Set

Last Updated : 24 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to get the maximum and minimum element in a set in Python, using the built-in functions of Python. Examples:

Input : set = ([8, 16, 24, 1, 25, 3, 10, 65, 55])
Output : max is 65

Input : set = ([4, 12, 10, 9, 4, 13])
Output : min is 4

max() in a Set

The built-in function max() in Python is used to get the maximum of all the elements in a set. 

Python3




# Python code to get the maximum element from a set
def MAX(sets):
    return (max(sets))
     
# Driver Code
sets = set([8, 16, 24, 1, 25, 3, 10, 65, 55])
print(MAX(sets))


Output:

65

min() in a Set

Python3




# Python code to get the minimum element from a set
def MIN(sets):
    return (min(sets))
     
# Driver Code
sets = set([4, 12, 10, 9, 4, 13])
print(MIN(sets))


Output:

4

Time complexity: O(n)
Auxiliary space: O(n), n is number of elements in set.


Previous Article
Next Article

Similar Reads

How to Get the Minimum and maximum Value of a Column of a MySQL Table Using Python?
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to get the Minimum and Maximum Value of a Column of a MySQL Table Using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an
2 min read
Python | Maximum and Minimum value from two lists
The problem of finding maximum and minimum values in a list is quite common. But sometimes this problem can be extended in two lists and hence becomes a modified problem. This article discusses shorthands by which this task can be performed easily. Let's discuss certain ways in which this problem can be solved. Method #1: Using max() + min() + "+"
7 min read
Python - Maximum and Minimum K elements in Tuple
Sometimes, while dealing with tuples, we can have problem in which we need to extract only extreme K elements, i.e maximum and minimum K elements in Tuple. This problem can have applications across domains such as web development and Data Science. Let's discuss certain ways in which this problem can be solved. Input : test_tup = (3, 7, 1, 18, 9), k
8 min read
Python Program for Maximum and Minimum in a square matrix.
Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix separately using linear search. Number of comparis
3 min read
Python | Maximum and minimum element's position in a list
Given a list of N integers, find the maximum and minimum element's position in the Python list. Example Input: 3, 4, 1, 3, 4, 5Output: The maximum is at position 6 The minimum is at position 3Maximum and Minimum Element Position in a ListBelow are the methods and ways by which we can find minimum and maximum element position in a Python List: Using
3 min read
Return the maximum of an array along axis 0 or maximum ignoring any NaNs in Python
In this article, we will see how to return the maximum of an array along axis 0 or maximum ignoring any NaNs in Python. ExampleInput: [[ 1. 0. 3.] [10. nan 30.] [nan 10. 20.]] Output: [10. 10. 30.] Explanation: An array with maximum values.nanmax method in PythonPython provides a nanmax method that returns the maximum of an array along a specified
3 min read
Return the maximum of an array or maximum ignoring any NaNs in Python
In this article, we will cover how to return the maximum of an array or maximum by ignoring any NaNs in Python using NumPy. ExampleInput: [ -1. -2. nan 1000.] Output: 1000.0 Explanation: maximum value of the array ignoring nans. One approach to use the built-in Python function max(), along with the filter() function and the math.isnan() function fr
4 min read
Maximum of XOR of first and second maximum of all subarrays
Given an array arr[] of distinct elements, the task is to find the maximum of XOR value of the first and second maximum elements of every possible subarray.Note: Length of the Array is greater than 1. Examples: Input: arr[] = {5, 4, 3} Output: 7 Explanation: All Possible subarrays with length greater than 1 and their XOR values of first and second
11 min read
Select row with maximum and minimum value in Pandas dataframe
Let's see how can we select rows with maximum and minimum values in Pandas Dataframe with help of different examples using Python. Creating a Dataframe to select rows with max and min values in Dataframe C/C++ Code # importing pandas and numpy import pandas as pd import numpy as np # data of 2018 drivers world championship dict1 = {'Driver': ['Hami
2 min read
Calculate the difference between the maximum and the minimum values of a given NumPy array along the second axis
Let's see how to calculate the difference between the maximum and the minimum values of a given NumPy array along the second axis. Here, the Second axis means row-wise. So firstly for finding the row-wise maximum and minimum elements in a NumPy array we are using numpy.amax() and numpy.amin() functions of NumPy library respectively. then After that
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg