Ridiculously Useful Python One-Liners  | Towards Data Science

Python offers a unique set of features!
Python programmers use them to write code quickly and in a more Pythonic way.
Here, I am discussing the 5 most powerful one-line codes which I mastered (and certainly you can) in 1 minute or less.
What do I mean by Powerful One-Liners??
These one-line codes will keep your program clean, clear, short, and simple to understand and modify.
If the one-liner is confusing, difficult to understand, then it is not a Pythonic way! So, use this powerful tool cautiously! ⚡️
Let’s jump in!
Depending on the purpose of the for loop ♻️, there are two ways of condensing it in a single line.
If the for loop body has only one statement, then simply write everything in one line.
for example, printing squares of all the numbers from 10 to 15 can be achieved in a single line as,
for k in range(10,16): print(k**2)
If the for loop is being used to populate or create a list, then use list comprehension.
In the same example as above, the list of squares of all the numbers can be created as,
The list comprehension, its pros, and cons are detailed with interesting examples in this quick read. 👇
towardsdatascience.com
In Python, you can assign multiple values to multiple variables in a single line. for example,
Moreover, you can assign the values of different types such as integer, float, string. 🔸 🔹 for example,
A step further, multiple values can be assigned to a single variable. for example,
As you can see above, all the values assigned to a single variable form a tuple.
Unlike other programming languages, Python eliminated the temporary variable while swapping the values of two variables.
Hence, the values of two variables can be swapped in a single line. for example,
Well, this is not limited to only two variables, rather it can be done with any number of variables. 🏆 Here is an example with three variables,
More about swapping values can be found here in — 3 Time-Saving Python Tricks.
Use Python Data structures 💡 efficiently to remove duplicates ️in a single line.
When you have a list of repeated values, then here is an elegant trick to remove the duplicates. Simply, convert the list into the set and it is done!
for example,
This single line of code has always saved a few lines in my code. ⏳
pyforest is the solution!!
pyforest is a python package which imports all the commonly used packages in a single line. It is not pre-installed, hence only for the first time use, you need to install it.
pip install pyforest
Rather than importing each and every library into your program, write below one line and you can avoid all the hustle of thinking and importing the required libraries.
import pyforest
After importing pyforest all the functions from frequently used Python packages such as pd.DataFrame(), np.arange(), sns.countplot(), and sys.exit() can be used directly.
dir(pyforest) will give you the entire list of 105 packages imported by pyforest. 🍀
Summing up,
I found these one-liners are quite handy while using Python for Data Analysis as well as complex automation tasks. Some of them such as pyforest are useful irrespective of the task (I do not advise using it, as it can make the code less readable if you are working on the collaborative project) while others such as for loop one-liner are good in some situations.
🚩 While using the one-liner you should keep in mind the PEP-8 guidelines about the maximum number of characters in a line i.e. 79 characters.
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!!