Ridiculously Powerful Functions in Python | Towards Data Science

Python offers a bunch of built-in functions!
Python programmers use them to write code quickly, clearly, and in a more Pythonic way.
Here, I am discussing the 5 most powerful, speedy built-in functions which I mastered (and certainly you can) in 1 minute or less. ⏳
I use them a lot in my projects to make the code faster, and more understandable way.
These built-in functions will keep your program clean, clear, short, and simple to understand.
Let’s jump in!
The map() function applies a specified function on each item in an iterable. Its syntax itself is self-explanatory.
map(function,iterable)
For example, let's make a function to return an input word in the upper case. And then map this function to each item in the list.
That's it! The function makeupper() is applied to each item in the list colors.
In my last project, the function map() was 1.5X faster ⚡ than a for loop to apply a complex function on each item in the list.
Here is an execution time comparison for a simple function ⏳.
Along with faster execution, map() function improves the readability of the code.
Sometimes, while working with iterators, we also need to have a count of iterations. ♻️
enumerate() is the solution!!
The enumerate() function adds a counter to an iterable and returns it in a form of an enumerated object. This object can be used directly in for loops and even can be converted into a list of tuples.
Here is its syntax: enumerate(iterable, start=0)
An iterable must be a sequence or some object which supports iteration. As Python is a 0-indexed language, the iteration counter starts at 0 by default. Certainly, you can change the starting number of the iteration counter.
Let’s continue with the colors example from the previous function.
colors = ['red', 'yellow', 'green', 'black']result = enumerate(colors)
In this example, the result is an enumerated object, which can be directly used in a for loop and even can be converted into a list.
The zip() function is used to use the similar index of multiple containers so that they can be used as a single entity.
The syntax is as simple as the definition: zip(iterables)
It produces a tuple with one item from each container or iterable.
Let’s have an example of two lists.
colors = ['red', 'yellow', 'green', 'black']fruits = ['apple', 'pineapple', 'grapes', 'cherry']
Using zip() function in a for loop,
zip() is often used in cases where the containers are assumed to be of equal length. However, if the containers have different lengths, the function zip() stops when the smallest container is exhausted. An example is below 👇
Using zip() function when containers have different sizes or lengths,
You can pass lists, tuples, sets, or dictionaries through the zip() function.
Often, we need to process an iterable and extract those items that satisfy a given condition.
filter() can save your time and efforts!!
The filter() extracts elements from an iterable such as list, tuples where the extracted elements are are the ones where the function returns true.
Here is a syntax:filter(function, iterable)
For easier digestion, let’s take an example. Let’s create a function to check if the word is in upper case or not. And then use the filter() to extract all the words from the iterable which are in uppercase.
Here is official documentation about filter().
🚩 Learn how to define a function in a single line using Ternary conditional.
Lambda functions are used to create anonymous functions i.e. functions without a name. They are useful when we need to create a function to do a single operation and can be written in one line. Its syntax is:
lambda parameters: expression
🎯 Lambda functions can have any number of parameters but can only have one expression.
For example, lambda x: x+2
That’s it!! Just now I created a function that takes a single input parameter x and adds 2 to it.
However, as you can see this is an anonymous function, it can not be called at a later stage. Hence, to call it any time in the program, lambda function can be assigned to a function object like this,
add2 = lambda x: x+2
Calling this function is exactly the same as calling a function that was defined with the def keyword. For example,
add2(10) # It will return 12 as output
Lambda function can be a good one-liner in some situations such as List comprehensions.
As shown in the above picture, newlist is generated with a single line of code by using the lambda function.
Summing up,
I found these built-in functions quite handy while using Python for Data Analysis as well as complex automation tasks. Some of them such as zip() , map() are useful in many cases while others such as Lambda function serve as the best one-liner in some situations.
🚩 5 Most Powerful One-Liners can even further boost your coding.
towardsdatascience.com
Now you can become a Medium member by signing up here to read all the stories published by me and other writers. If you do so, I will get a small portion of your fees. Feel free to join my email list to stay updated about my writing.
Thank you for reading and investing your time!!