When I first started coding in Python, I focused on executing. I wasn’t concerned about efficiency or clean code. If it worked, I was satisfied. As a result, I became over-dependent on workarounds instead of learning best practices.
Many of these best practices are found in the Python documentation. Thanks to the documentation, I discovered built-in functions which solve many problems I had previously deemed as annoying, yet unavoidable Python quirks.
This isn’t meant to be a list of the most used builtin functions. Rather, it’s a list of functions I wish I learned and embraced when I first started coding. I guarantee they will simplify and improve your code once you learn how to use them.
Best Built-in Functions
1. Enumerate
The enumerate function takes an iterable (such as a list, set, or Series) as an input and returns an enumerate object, which contains tuples of the count and value of each item in the input. Since the output is also an iterable, enumerate requires a for loop or list wrapper to iterate on and process the items.
The start argument sets the start value for the index values. For example, to create a list of weekdays for this week where the index value corresponds to the date, use start = 25.
enumerate is especially useful in for loops that use both the index and value of each item. Before, I used range(len(list)) in for loops and then created variables for the index and item in the body of the loop. Now, I use enumerate, which saves typing time and creates cleaner code.
2. Zip
zip takes one or more iterables as an input and iterates over them in parallel. It outputs an iterator of tuples with each tuple containing one item from each input. In other words, zip joins the inputs to create tuples where the i-th tuple contains the i-th item from each of the inputs.
The inputs do not need to be the same length. If they have different lengths, the output will be the same size as the shortest input.
Like enumerate, zip returns an iterable, which requires an iterable like a for loop or list wrapper to unpack the items.
In cases where inputs are expected to be the same length, use the strict = True argument. If inputs have different lengths, a ValueError is raised.
When given only one input, zip returns tuples of length 1.
3. Map
map takes a function and one or more iterables as inputs, applies the function to the iterable(s), and return the results. Like zip and enumerate, the output is an iterable.
Note, list comprehension achieves the same result.
However, map offers advantages over list comprehension when the function has multiple inputs. For example, imagine you have lists of all the new Medium subscribers following your account. However, the lists are broken up by week, and you want to find the maximum subscribers gained on each weekday.
List comprehension alone cannot replicate this. However, list comprehension would work if it called other functions like zip.
4. Sorted
The sorted function returns a new sorted list from an iterable input. If you ever encountered a None object when working with lists, this function is made for you. Typically, these errors result from assigning a variable to an inplace operation, such as list.sort() or list.reverse(). Inplace operations don’t output anything so there’s nothing for the variable to store.
The sorted function returns an output, which can then be stored as a variable.
I didn’t know about this function for an embarrassingly long time. Instead, I copied a list and then sorted the copy in place. If I knew about sorted, I could have avoided this time-consuming workaround.
Conclusion
Thank you for reading my article. If you enjoy my content, please consider following me. Also, all feedback is welcome. I am always eager to learn new or better ways of doing things. Feel free to leave a comment or reach out to me at [email protected].






