You are currently viewing Python One-Liners To Enhance Code Quality

Python One-Liners To Enhance Code Quality

Python is known for its simplicity and readability. Python provides flexibility to write complex code straightforwardly, and writing code in Python is like writing the English language.

Python is popular because of its self-explanatory syntax and power to restrict long programs to a few lines. One-liners are one of the powers Python has, allowing users to write a single-line code that runs multiple tasks simultaneously.

This article will show some one-line Python programs that help us in everyday programming, enhance productivity, and speed up our coding process.

List Comprehension

We can use list comprehension for writing a multiline set of Python statements in a single line. We can write the for loop with an if-else condition in a single line instead of writing in multiple lines.

We can wrap the above code in a single line using list comprehension.

Lambda Function

The Lambda function is also known as the anonymous function. We can define an anonymous function using a lambda keyword and then provide an expression.

Creating an array from random numbers

An array is a collection of elements of the same data types stored in contiguous memory locations with assigned index values that help locate them.

The above code generates a 3D array of shapes (2, 3, 2) from random numbers between 0 and 16.

Using walrus operator

This operator is used to assign values to the variables within an expression. It is an assignment operator represented by the sign :=. This feature was added in Python version 3.8.

We have a Python program that takes the input from the user, if that input is 7, it will print something, if not, it will return nothing.

The above code can be written in a single line.

Generating random numbers

The most common way of generating random numbers is by using a library called random.

This code will generate a list of 10 random numbers between 1 and 99.

Substitute any pattern

We can use the RegEx (Regular Expression) to substitute any pattern. Let’s say we have text data in which we have to substitute letters starting and ending from * (asterisk).

text = "The *big* data got bigger and bigger. It is called *big* data because of having *big* data"

The code to substitute the above pattern.

The pattern \*(.*?)\* matches the characters starting and ending with *, and it has a group capture that captures the text between *.

Remove duplicates

To remove the duplicate values from the Python list, convert that list to the set. Consider the following example to understand it better.

All the duplicate values were removed because the set doesn’t allow identical values.

Replacing a word

When working on textual data in Python, the replace() method can come in handy to replace a particular text with a text of your choice.

text = “The big data got bigger and bigger. It is called big data because of having big data”

The above code will replace the word data with dataset.

Multiple inputs simultaneously

If you are working on a project that wants to take multiple user inputs simultaneously, the following one-liner will come in handy.

The user can enter unlimited inputs and get those in the form of a list because the split() method will convert the string input into a list.

Multiple variable assignments

Using a comma, we can assign multiple values to the variables simultaneously. A value of any data type can be assigned to the variable.

We assigned a set to variable a, a float to variable b, and a string to a variable c.

Writing in a file

We can write some data into a file by opening it with the open() function and then using the write() method.

Reading a file

In the above code, first, we open a file and use the for loop to read the content of the file line by line.

Numbering each item in a list

We can add a counter to each item in the existing list using Python’s built-in enumerate() function.

We can start counting from any number by providing the starting number to the start argument.

Merging items from the lists

We can merge two iterators using the zip() function. It takes the iterable and iterates them parallelly, producing tuples of each item from the iterable.

Sorting a list

We can sort a list in ascending or descending order. Let’s say we have a list of names.

my_lst = ["Rishu", "Rishi", "Yashwant", "Abhishek", "Sachin", "Yogesh"]

To sort the above list in ascending order(alphabetically), we have to write the following code.

We can use the reverse=True inside sort method to sort the list in descending order.

Copying an entire directory or a file

Copying files or even an entire directory can be overwhelming. We can automate the task of copying files or directories using the shutil module in Python.

If we wanted to copy only the files.

List the directories

Python os module provides a method to list the directories of the specified path.

Swapping variables

If we use a traditional approach, we will write a Python program like the following.

Instead of using the two methods mentioned above, we can do it in a single line using commas.

Reverse a list

We can use the reverse() method to reverse the order of the elements in the list.

Conclusion

The one-liners we’ve seen above help us in everyday programming and enhance our code quality. These single-line codes can save us lots of time.