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]
- 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))
- 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)
- 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)
- 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])
- 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)
- 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])
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])
❤️ If you enjoyed this article, that motivates to write more!
🦄 If you truly consider this article helped you wholly!
🔖 If you need to check back on this article later on.
🤔 Please leave your comment, your opinion very important.
See you next time, buddies! Don't forget to share this post among all of your friends!
Discussion (0)