One of the first algorithms that we are usually taught is finding either the smallest or the largest element in a list of elements by iterating through every element in a list.
Let’s assume that we are trying to find the smallest element in a list.
The algorithm is quite straightforward and intuitive even for a 5-year-old: You assume that the first element is the smallest and save its value in a variable.
You then keep on comparing this value with all the elements in the list from the first one until the last one checking whether there is another element that is smaller.
In plain Python, this implementation looks like the following:
Finding the largest element is analogous to this one:
Now there is a built-in function for finding the largest and the smallest element in a list in a single line.
You don’t need to do any additional imports, or anything else. Just call the max or the min function with the list and you are basically done:
These are just two of the many other helpful built-in functions that you can use in Python, but let’s leave it there for now.
I hope you find this useful.