Powerful One-Liner Python codes

Python is a widely-used general-purpose, high-level programming language. Python programs generally are smaller than other programming languages like Java. Programmers have to type relatively less and indentation requirements of the language makes them readable all the time. However, Python programs can be made more concise using some one-liner codes. These can save time by having less code to type.

Refer to the below articles to know more about Python.

One-Liners in Python

One-Liner #1: To input space separated integers in a list:

Suppose you want to take space separated input from the console and you want to convert it into List. To do this map() function can be used that takes int() method and input().split() methods as parameter. Here the int() method is used for conversion of input to int type and input().split() methods are used to take input from the console and split the input by spaces. Below is the implementation.

filter_none

edit
close

play_arrow

link
brightness_4
code

lis = list(map(int, input().split()))

chevron_right


Note: To know more about Python map() function click here.



One-Liner #2: To input a 2-D matrix(When the entries are given row-wise):

The most naive method that comes in mind while taking a input for 2-D matrix is given below.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Input for row and column
R = int(input()) 
C = int(input()) 
  
matrix = [] 
  
# for loop for row entries 
for i in range(R):          
    a =[] 
  
    # for loop for column entries 
    for j in range(C):
         a.append(int(input())) 
    matrix.append(a) 

chevron_right


The above code can be written in one-line that is way more concise and saves time especially for competitive programmers.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Input for row and column
R = int(input())
C = int(input())
  
# Using list comprehension for input
matrix = [[int(input()) for x in range (C)] for y in range(R)] 

chevron_right


One-Liner #3: We know this fact, but sometimes we tend to ignore it while translating from other languages. It is swapping of two numbers. The most naive way of doing this is:

filter_none

edit
close

play_arrow

link
brightness_4
code

temp = a
a = b
b = temp

chevron_right


However, Python provides one-liner for this also. The Pythonic way is:

filter_none

edit
close

play_arrow

link
brightness_4
code

# to swap two numbers a and b
a, b = b, a

chevron_right


One-Liner #4 Lambda Functions(Anonymous Functions) – Lambda functions are python one liner functions and are often used when an expression is to be evaluated.

For example, let’s suppose we want to create a function that returns the square of the number passed as argument. The normal way of doing this is:

filter_none

edit
close

play_arrow

link
brightness_4
code

def sqr(x):
    return x * x
  
print(sqr(5))

chevron_right


Output:



25

Lambda function replaces a function wherever a single expression is to be evaluated.

filter_none

edit
close

play_arrow

link
brightness_4
code

sqr = lambda x: x * x
print(sqr(5))

chevron_right


Output:

25

One-Liner #5 List comprehensions – This is a concise way to create lists. Instead of doing it the usual way, we can make use of list comprehensions.

For example, we want to create a list of even numbers till 11. The normal way of doing this is:

filter_none

edit
close

play_arrow

link
brightness_4
code

evenNumbers =[]
for x in range(11):
    if x % 2 == 0:
        evenNumbers.append(x)
  
print(evenNumbers)

chevron_right


Output:

[0, 2, 4, 6, 8, 10]

The pythonic way:

filter_none

edit
close

play_arrow

link
brightness_4
code

evenNumbers =[x for x in range(11) if x % 2 == 0]
print(evenNumbers)

chevron_right


Output:

[0, 2, 4, 6, 8, 10]

One-Liner #6: This trick may help while using if-else or while loop. Instead of doing this –

filter_none

edit
close

play_arrow

link
brightness_4
code

if m == 1 or m == 2 or m == 3:
    pass

chevron_right


We can code it as:

filter_none

edit
close

play_arrow

link
brightness_4
code

if m in [1, 2, 3]:
    pass

chevron_right


One-Liner #7: There are various ways to reverse a list in python.
Pythonic ways to reverse a list: