Finding biggest element from a list

Python provides max() function for finding biggest element from a list.

max() function returns a biggest element from a list.

There is also other way of finding biggest element from a list instead of using max() function. We can apply some logic to find biggest element from a list.

Using max() to find biggest element from a list.

#First of all create a list of numbers
num = [10,20,30,45,33,55,77,99,2,300]
#Using max() function to find biggest element and assigning biggest element to a variable n1
n1 = max(num)
#Printing entire elements from a list
print('Print entire elements from a list',num)
#Printing biggest element from a list
print('Biggest element from a list',n1)

Output

Print entire elements from a list [10, 20, 30, 45, 33, 55, 77, 99, 2, 300]
Biggest element from a list 300

Finding biggest element by using some logic

First creating a number list.

num = [1,200,33,222,999,33333,8989]

Elements in above list is represented as below
num[0] = 1
num[1] = 200
num [2] = 33
num[3] = 222
num[4] = 999
num[5] = 33333
num[6] = 8989

Finding length of a list and printing the length

l = len(num)
print('Number of element in a list',l)

Assuming the first element of a list is the biggest element. Assigning value of a first element to variable big

big = num[0]

Now using for loop to compare all elements. If any element found bigger that element is assign to a big variable

for i in range(1,l):
    if num[i] > big:
        big = num[i]

At last printing the biggest element from a list

print("Biggest element from a list :",big)

Output

Number of element in a list 7
Biggest element from a list : 33333

Complete Program

num = [1,200,33,222,999,33333,8989]
l = len(num)
big = num[0]
print('Number of element in a list',l)
for i in range(1,l):
    if num[i] > big:
        big = num[i]

print("Biggest element from a list :",big)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s