DEV Community

Cover image for 6 ways to find the largest number in a python list.
Alvison Hunter Arnuero | Front-End Web Developer
Alvison Hunter Arnuero | Front-End Web Developer

Posted on • Updated on

6 ways to find the largest number in a python list.

Howdy, folks! I certainly hope you are having a great time and staying safe from that ugly Covid19 virus! Have you ever wonder what'd be the best approach to find the largest element of a list of numbers in python? Well, let's find out this together, buddies.

As you might've already guessed, know or even imagine, there are certainly several ways to find the largest number in a list in python, as in any other coding scenario. Let's break down some of the most common ones I've used when it comes to this type of situation. Are you ready, pals?

6 most common solutions

First, let's create a simple integer numbers list.

numbers_lst = [1, 2, 3, 4, 50, 6, 3, 2, 3, 8]
Enter fullscreen mode Exit fullscreen mode
  • Using the max() Function Now is time to put together a couple of those approaches into action. Let's start with the fastest solution to this:
# 1- Fast approach using built in max function
print("Using Max Built-in function: ", max(numbers_lst))
Enter fullscreen mode Exit fullscreen mode
  • Find the largest element manually For some other pals out there, they would have preferred to built this solution by manually looking for that largest element, well, let's code something like that as follows:
# 2- Manual Approach iterating all the elements of the list
max_num = numbers_lst[0]
for n in numbers_lst:
    max_num = n if n >= max_num else max_num
print("Manual Iteration: ", max_num)
Enter fullscreen mode Exit fullscreen mode
  • Find the largest item using list comprehension Well, that was that, wasn't it? If you find that a bit too large or too much code to write, let's simplify things on this case by using the list comprehension approach, shall we?
# 3- Using comprehensions, in this case for the list
max_num = numbers_lst[0]
[max_num := n for n in numbers_lst if n >= max_num]
print("List Comprehension: ", max_num)
Enter fullscreen mode Exit fullscreen mode
  • Sorting list ascendantly & getting the last item Oh yeah, cowboys, that was a bit too short, and for some, a bit hard to read and understand, well, that is the magic of python, it has colors for every taste: Are you that kind of guy that prefers to go as simple as possible? Well, I am! So let's get back to the matter but with a simpler solution, bud!
# 4- Sort the list in ascending order and print the last element in the list.
numbers_lst.sort()

# printing the last element, which is in this case the largest one
print("Using the sort list method:", numbers_lst[-1])
Enter fullscreen mode Exit fullscreen mode
  • Using built-in sorted() function Hey, now, do you still think that was too iffy for a solution? Well, don't worry, let's add another cup of coffee to this breakfast, shall we? Let's use the sorted built-in function.
# 5 - Using the built in sorted function and getting
# the last list element afterwards
sorted_lst = sorted(numbers_lst, reverse=True)
max_num = sorted_lst[0]
print("Sorted List: ", max_num)
Enter fullscreen mode Exit fullscreen mode
  • Using a reverse list shortcut Last, but not least, this method is not 100% recommended because it might not give you the expected results in some versions of python, or the compiler as well, nevertheless, it is worth to bring it to the table.
# 6 - Using the built-in reversed function to reverse
# the list, convert it to a list and print first item
print(list(reversed(numbers_lst))[0])

# Another nice shortcut for this approach should be this:
print(numbers_lst[::-1][0])
Enter fullscreen mode Exit fullscreen mode

Final Source Code

Now that we have fractionated all of these solutions, let's put together a final source code fo you guys to test it out and play with it. Please leave your comments with some other approaches or better solutions, to update the post if needed. Thank you so much for reading us, folks!

numbers_lst = [1, 2, 3, 4, 50, 6, 3, 2, 3, 8]

# 1- Fast approach using built in max function
print("Using Max Built-in function: ", max(numbers_lst))

# 2- Manual Approach iterating all the elements of the list
max_num = numbers_lst[0]

for n in numbers_lst:
    max_num = n if n >= max_num else max_num

print("Manual Iteration: ", max_num)

# 3- Using comprehensions, in this case for the list
max_num = numbers_lst[0]
[max_num := n for n in numbers_lst if n >= max_num]
print("List Comprehension: ", max_num)

# 4- Sort the list in ascending order and print the last element in the list.
numbers_lst.sort()

# printing the last element, which is in this case the largest one
print("Using the sort list method:", numbers_lst[-1])

# 5 - Using the built in sorted function and getting
# the last list element afterwards
sorted_lst = sorted(numbers_lst, reverse=True)
max_num = sorted_lst[0]
print("Sorted List: ", max_num)

# 6 - Using the built-in reversed function to reverse
# the list, convert it to a list and print first item
print(list(reversed(numbers_lst))[0])

# Another nice shortcut for this approach should be this:
print(numbers_lst[::-1][0])

Enter fullscreen mode Exit fullscreen mode

Thanks for reading this article, I hope you enjoyed it as much as I did writing it. Until next time, dear readers!

❤️ Your enjoyment of this article encourages me to write further.
💬 Kindly share your valuable opinion by leaving a comment.
🔖 Bookmark this article for future reference.
🔗 If this article has truly helped you, please share it.

Top comments (2)

Collapse
 
iamsecular1947 profile image
iamsecular

max_num = numbers_lst[0]
[max_num := n for n in numbers_lst if n >= max_num]
print("List Comprehension: ", max_num)
Doesn't work, can anyone help?

Collapse
 
alvisonhunter profile image
Alvison Hunter Arnuero | Front-End Web Developer

You are probably missing the list itself on this example, check this out:

numbers_lst = [9,8,6,12,34,56,77]
max_num = numbers_lst[0]
[max_num := n for n in numbers_lst if n >= max_num]
print("List Comprehension: ", max_num)

# another way, more easy than the first one:
print("Max item value is: ", max(numbers_lst))
Enter fullscreen mode Exit fullscreen mode