Python Program to Find Largest Number in a list without max()

Python Program to Find Largest Number in a list without max()

In this article, we will be going to find the largest number in a list without using the built in max() function. By solving such type of simple problems will help you to build up your logic building skills. If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python Program to Find Largest Number in a list without max()


Python Program to Find Largest Number in a list without max()


Logic and Explanation

Question - Write a Python Program to Find Largest Number in a list without max()

Python Program to Find Largest Number in a list without max() - In this program we have to find the maximum number present in a list without using the in-built list max() method. For this there can be multiple ways and we can use many different logics to do this.


First thing which we can do is to sort out the list in ascending or descending order. Let's say once we sorted the list in ascending order, then the largest number will be at the last place at the last index of the list. Thereafter, we can call it from list and if we arrange it in descending order then the largest number will be at the first position.

So, this is one way, firstly let's write the code for the above logic. Consider a list L containing some numbers as shown below.

L = [1,5,39,59,92,33,58,20]
L.sort()
print("Largest Number", L[-1])


This is one of the way to find the largest number in a list. Another way can be that let's assume that the number at index 0 i.e. the first number is maximum. Then we will iterate through the list and at each step of the iteration we will check for the condition that whether the number in iteration is greater than that we consider the maximum. If yes, then we will change the value of our variable maxelement.


As the loop will end, our variable will contain the largest number. Here's the code for the same.

L = [1,5,39,59,92,33,58,20]
maxelement = L[0]
for x in L:
    if x > maxelement:
        maxelement = x
print(maxelement)


I hope the logic and all the source code is clear to you. In case of any doubt feel free to ask in the comments below.

So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post