# Python Program to Get Minimum and Maximum from a List

Python is an interpreted, object–oriented, high level, programming language with dynamic semantics. Developed by Gudio Van Rossum in 1991.

List is a data structure in python, that is changeable, mutable and iterable ordered sequence of element. They are used to store multiple elements in a single variable.

List can be created by using square brackets. By placing elements inside square bracket [], separated by commas.

For example: list1= [“a”,”b”]. There is a one more type of list known as “Nested list”. It is list within list.

list1=[“a”,”b”[1,2]”c”]


## How To Get Minimum From List

Let’s learn how to get minimum and maximum from a list. It is important to keep in mind that the given elements are in list, and that list consists of only integers or variables. Mixed list are not allowed in this programing.

• In python there are many ways to get minimum and maximum from a list by using different method such as using min() and max() function, by using “for” loop, and by using sort()function.

• Here, we have given a list of numbers and we have to find the smallest number in given list by using different methods, such as min(), for loop(), and sort(). We can use these three methods to get minimum from the List.

### Using min() Funtion

The min() function returns the value of highest elements from list or lowest value in an iterable. It is used to find lowest value of strings as well numbers.

### Example

The following example finds the smallest element in a list of numbers (15, 58, 30 and 97) and prints it out. The variable ‘x’ is assigned the value of the minimum number in the list which is 15.

a=[15,58,30,97]
x=min(a)
print("the smallest element is",x)


### Output

the smallest element is 15


### Example

In this example, the min() function is used in two different ways. In the first program, it returns the item with the lowest numerical value from a tuple. In the second program, it returns the name with the lowest alphabetical value.

a=["alina" , "misti", "pallvi"]
x=min(a)
print("the smallest element is",x)


### Output

the smallest element is alina


### Using “for” Loop ()

for” loop is used is for repeating over a sequence again and again. With “for” loop we can execute set of statement, once for items in a list, tuple , set , etc. “for” loop() function is used for finding both minimum and maximum values from list. Here, we are going to find minimum value from “for” loop.

### Example

In the following example, we set the initial minimum value (m=l) so that m is assigned to the smallest value in the list. Inside the loop, 'i' takes each value of the list one at a time and compares it to 'm'.

If 'i' has a smaller value than 'm', then this new value is used as m. After iterating through all elements in turn, we are left with the output being equal to the smallest number from our list.

l=[12,78,89,76,32]
m=l
for i in l:
if i<m:
m=i
print(" the smallest element is:",m)


### Output

the smallest element is: 12


### Using sort() Function

The Python sort() function can be used to sort a list in ascending, descending, or user-defined order. By default, it will sort the list in alphabetical order from A-Z to a-z.

### Example

In the following example, we are using the sort() function to sort the elements in the list given below, the list has numbers.

list=[56,78,90,67]
list.sort()
print(list)


### Output

On executing the above program, we get the output as follows, using sort() arranges the numbers in ascending order and prints them as follows -

[56, 67, 78, 90]


### Example

In the following example, we are using the sort() function to sort the elements in the list given below, the list has Alphabets.

list=["semuel","ashish","henry","varun"]
list.sort()
print(list)


### Output

On executing the above program, we get the output as follows, using sort() arranges the alphabets in ascending order and prints them as follows -

['ashish', 'henry', 'semuel', 'varun']


## How To Get Maximum From List

Here, we present a list of numbers and explain how to find the smallest number in the given list using three different methods: max(), for loop(), and sort(). These methods can be used to identify the minimum value from a list.

### Using max() Function

The max() function returns the value of highest elements from list or highest value in an iterable. It is used to find highest value of strings as well numbers.

### Example

This example creates a list of numbers (15, 58, 30 and 97) and then it uses the max() function to find the highest number in the list.

a=[15,58,30,97]
x=max(a)
print(x)


### Output

97


### Example

This example finds the longest string in a list of strings called "names" and assigns it to variable x. The max() function is used to compare each string in the list and find which one has the most characters.

names=["krishna","asif","david","yash"]
x=max(names)
print(x)


### Output

Yash


## Using “for” Loop()

The "for" loop is used to iterate over a sequence multiple times. This type of loop allows us to execute a set of statements for each item in a list, tuple, set, etc.

Additionally, the "for" loop can be used to find both minimum and maximum values from a list. In this example we will use the "for" loop function to determine the maximum value in the list.

### Example

The following example finds the largest number in a list. It starts by creating a list called 'l' with 5 numbers, and then assigning the first item in that list to the variable 'm'.

It then loops through each of the items in 'l', comparing it to the value held by 'm'. If any of them is larger than what's currently stored in m, it replaces it with that new number.

The loop ends when all numbers have been compared and at this point, 'm' holds the highest number from the original list.

l= [12,78,89,76,32]
m= l
for i in l:
if i>m:
m=i
print(m)


### Output

89


## Using sort() Function

The sort() function can be used to sort a list in ascending, descending, or user-defined order. By default, the sort() method gives the sorted values of the list in ascending order. Therefore, when we need to retrieve the largest value from a list, we must use (list[-1]). This function can also be used to arrange items in alphabetical order from A-Z and from a-z.

### Example

The following example takes a list, list1, which contains four elements: 67, 89, 9 and 76. It then sorts the elements from lowest to highest using sort() function.

list1=[67,89,9,76 ]
list1.sort()
print("largest element is :",list1[-1])


### Output

largest element is : 89


### Example

See the following example for better understanding -

list1=["akirti","riya","zaid","david"]
list1.sort()
print("largest element is :",list1[-1])


### Output

largest element is : zaid


## Conclusion

In this article, we have briefly discussed the methods to get minimum and maximum from list.

Updated on: 21-Apr-2023

768 Views 