Python One Liners

Introduction

The article covers 10 good Python one-liners, which can help in speeding up coding or help in code maintainability perspective. There are many such one-liners in Python, but the article highlights 10, I have excluded comprehensions type one-liners from the list as those will be covered in a separate article on comprehensions.

One-Liners which are covered in articles are,

  1. HTTP Server
  2. Pretty Print JSON
  3. Walrus Operator
  4. Reverse List
  5. Finding Max Number
  6. Matrix Transpose
  7. Import libraries in One line
  8. Finding subsets of the set
  9. Swap
  10. For loop append

HTTP Server

Sometimes we need a small web server for quick and fast testing, python provides a small lightweight web server that can be started in just one line.

python -m http.server #Python3
python -m SimpleHTTPServer 8000 # Python2

Pretty Print JSON

Python has a module named ‘json’, the module has method ‘dumps’ which simply takes the JSON object and pretty prints it.

json_obj = '[{"id":50,"emp_name":"Sam"},{"id":40,"em_name":"David"}]'
print(json.dumps(json_obj, sort_keys=True, indent=4))
{
    "emp_name": "Sam",
    "id": 50
}, {
    "em_name": "David",
    "id": 40
}]

Walrus Operator

Walrus operator is an assignment expression, it allows us to assign a value to a variable inside an expression.

Before

id = 10
print(id) # 10

Now

print(id:= 10) # 10

Reversing a List

The list in Python can be reversed using slicing technique or using the in-built reverse function.

l = [1,2,3]
print(l[::-1]) # 3,2,1

Or

print(list(reversed(l))) # 3,2,1

Finding Max Number

It can be done using custom comprehension or simply by using the ‘max’ function.

l = [1,2,3]
max(l) # 3

Matrix Transpose

Matrix transpose can be achieved using the zip function or using the NumPy .T attribute, Let’s check using the zip function.

matrix=[(1,2,3),(4,5,6),(7,8,9),(10,11,12)]
matrix # [(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)]

for i in zip(*matrix):
    print(i)

#(1, 4, 7, 10)
#(2, 5, 8, 11)
#(3, 6, 9, 12)

Import libraries in One line

PyForest is an open-source library that is particularly developed for Lazy programmers, it imports all the major libraries at once that are required for python programming.

Since Pyforest is a separate library, we need to first install it using pip or anaconda. First, we need to import the pyforest, which will only import the required library

from pyforest import * 

then magic happens at step 2.

lazy_imports() 

This line will import all the required libraries.

Finding subsets of a set

This can be achieved using the itertools library of Python.

list(itertools.combinations({4,5,6}, 2)) 
# [(4, 5), (4, 6), (5, 6)]

The above one-liner is creating sets of {4,5,6} into 2.

Swap

x=10
y=20
x,y = y, x
print(x, y) # 20 10

Swap magic happens when x, y = y, x is executed.

For loop append

Say we have 2 lists, and we want to append all the iterable to one single list, the list ‘extend’ function can be used.

l1 = [1,2,3]
l2 = [4,5,6]
l1.extend(l2)
l1
#[1,2,3,4,5,6]

Summary

The article tried to highlight some of the most important one-liners, intentionally for and list comprehensions is excluded as it will be covered separately. 


Similar Articles