Member-only story
Learn Powerful Python one-liners
Today, we are going to learn about useful Python one-liners. When I first saw a single-line code fragment in Python I was like, “Is this possible?” one-liners reduce the time to write a lot of code and make it cleaner but it may be difficult to understand for those who just started their journey in Python.
5 min readMar 21, 2022
My article is for everyone! Non-members can click on this link and jump straight into the full text!!
We are going to see the example of Python one-liners with for loop, if, else statements, built-in function (map() and filter()) and anonymous Lambda function.
One-liner
1. One-liner for loop
Normal Program
num_list=[]
for num in range(0, 11):
num_list.append(num)
print(num_list)
## >>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
One-liner
num_list=[num for num in range(0, 10+1)]
## '10+1' as the upper limit is excluded.
print(num_list)
## >>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Let us start with a simple Python one-liner program where we create a list that contains…