Abhirajshri Winsome
Abhirajshri Winsome

Reputation: 75

For loops vs Max function for finding the largest number in a list in Python (Beginner)

What is the difference between these both? For loop and Max Function. Which one should I choose? For loop:

numbers = [3, 6, 2, 8, 4, 10]
max = numbers[0]
for number in numbers:
    if number > max:
        max = number
print(max)

Instead, why can't I use the max function, which is only 1 line of code? Max function:

print(max(3, 6, 2, 8, 4, 10))

Both of them show the same thing on the terminal. Why can't I choose this one?

Upvotes: 1

Views: 2552

Answers (5)

Yang Liu
Yang Liu

Reputation: 346

Suggest to use built-in max(), it not only saves your coding time, but also less-error-prone and faster.

Why not make life easier :)

Upvotes: 1

anvoice
anvoice

Reputation: 335

The max function runs faster usually than an equivalent for loop. Running timeit, I get:

>>> import timeit
>>> print(min(timeit.Timer('max((1,2,3,4,5,6,7,8,9,10))').repeat(100,10000)))
0.0017686000001049251

>>> print(min(timeit.Timer('''max = 1
for number in (1,2,3,4,5,6,7,8,9,10):
    if number > max:
        max = number''').repeat(100,10000)))
0.0028327999998509767

The numbers printed are the execution times in seconds, for a minimum of 100 trials of 10000 repetitions of finding max both ways. As you can see, max is faster. The reason someone decided to use a for loop in a tutorial is probably to illustrate the idea of for loops to beginners.

Upvotes: 3

Dan
Dan

Reputation: 159

When you use the max() function, you're using a built-in function that comes with python. These so-called built-in functions are generally faster than if you coded a solution for that exact same task yourself.

max() takes as input an iterable, meaning: a list, tuple, or just values separated by commas.

In your code, you're trying to achieve the same thing as the max function, but it would only work when given a list or a tuple as input numbers. Look at these examples below:

my_tuple = (2,5,6,1,6)
print (max(my_tuple))
>> 6

my_list = [1,2,3,3,8]
print (max(my_list))
>> 8

print (max(5,1,2,5,3,4))
>> 5

It should be useful to use implement the max() function for most if not all scenarios.

Upvotes: 0

mhhabib
mhhabib

Reputation: 3121

Of course, You will use max() it is O(n) in term of Big O notation unless you are using a different data structure supporting the max of a value collection due to some implementation invariant.

Finding a maximum element using a loop, it will iterate over all the element and that case loop also will take O(n). If you have no limitation o using max() then you can use max() which is easy and one linear.

You can also go through about max() efficiency.

Upvotes: 0

Ethan Zerad
Ethan Zerad

Reputation: 46

You should use the max() function. It requires fewer lines of code, and it has a way, way smaller footprint than a for loop. There are no cons to using the max() function, it will work exactly the same way as a for loop would, just with less lines of code. Nothing should stop you from using this built-in method. As far as optimization and speed go, I believe there won't be a significant difference using either.

Upvotes: 1

Related Questions