0

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?

mhhabib
  • 2,975
  • 1
  • 15
  • 29
  • You can certainly choose the max function. I don't know off the top of my head which will run faster, but I believe the max function is probably optimized to run at least about as quick as the for loop. Btw, the "Pythonic" way is often considered to write code simply first, then optimize later if necessary. Point is, nothing is stopping you from using the built-in. – anvoice Jan 13 '21 at 06:07
  • @anvoice But in a tutorial of Python in YouTube, he didn't use the max function. Rather he used the For loop function. Link: https://www.youtube.com/watch?v=_uQrJ0TkZlc&t=11996s. He showed that in 2:01:48 (Timestap) – Abhirajshri Winsome Jan 13 '21 at 06:12
  • You can check this topic to see behind the magic : https://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions – Nott Jan 13 '21 at 06:17
  • Please answer as a beginner. I can't understand that high level. – Abhirajshri Winsome Jan 13 '21 at 06:22
  • max() runs faster and is easier to code. He used a for loop to illustrate for loops to beginners. – anvoice Jan 13 '21 at 06:29

5 Answers5

1

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.

Ethan Zerad
  • 56
  • 1
  • 3
  • But in a tutorial of Python in YouTube, he didn't use the max function. Rather he used the For loop function. Link: youtube.com/watch?v=_uQrJ0TkZlc&t=11996s. He showed that in 2:01:48 (Timestap) – Abhirajshri Winsome Jan 13 '21 at 06:21
  • 1
    Keep in mind that this tutorial is made for beginners, therefore, the instructor breaks it down into more lines of code to describe the process. Instructors don't tend to use shorter methods right off. As you code more and you use more methods, you'll become a more efficient programmer. Using the max() function rather than a for loop will only make you a more efficient programmer, and is likely to help you avoid bugs in a large program. – Ethan Zerad Jan 13 '21 at 06:23
1

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.

anvoice
  • 295
  • 2
  • 7
1

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 :)

Yang Liu
  • 346
  • 1
  • 6
0

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.

mhhabib
  • 2,975
  • 1
  • 15
  • 29
0

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.

Dan
  • 160
  • 1
  • 6
  • 19