How to do everything in one line in Python

ViscousPotential
Analytics Vidhya
Published in
3 min readFeb 22, 2021

--

This is part of a series discussing everything required to get an all-encompassing grasp of the Python programming language in as short a time as possible. Whether you are a beginner or an expert, I intend for you to learn something new.

When writing python code, you may want to make your code shorter. This could be due to character restraints or to make the code look cleaner. These tricks will be specifically useful for coding challenges like Clash of Code where you win by having fewer characters.

Swapping Variables

A line that will get a lot of mileage is one that swaps variables without the use of temporary variables. In Python you can simply use the multiple assignment notation.

The first method of minifying an IF statement is to simply move the all the code onto one line. This assumes that there is only one operation to be run and that you don’t need to use an else. Usually it isn’t advised to do this at all.

The second method is by writing the result if the condition is true followed by “if” the condition. This can further be followed by an “else” and the result on false if you require a shortened if/else statement. As shown this whole statement can be put after a variable declaration or inside a print statement and only the result will be set or printed.

The final, shortest and, in my opinion, best method is to make use of lists. A lot of people say that it is bad practice to use if/else statements, so this is a functional way of getting rid of them entirely. The code is written by creating a list with the false result as the 0th term and the true result as the 1st term. You can then use the condition to extract the appropriate result from that list.

This works because the condition can only be either True or False. In code True and False are exactly equivalent to 1 and 0. Therefore the condition can be considered to only either be 1 or 0. This means we can address the 0th or 1st term in any array using just the condition.

This can even be taken a step further by putting lambdas inside the arrays to call functions or run code and addressing them via the condition before running them with”()”, all in one line.

A for loop in one line is sometimes called list or dictionary comprehension because it usually outputs a list or dictionary.

Python has two functions that closely match the results of list comprehensions. The first of these is the MAP function. Normal list comprehension achieves the same thing and essentially creates a new list where every element in an input list has been modified by a function or operation.

The second is the FILTER function, which is replaced by conditional list comprehension. This simply does the same as normal list comprehension. Importantly, it additionally filters out items that you don’t want included in the resulting list with an if statement.

Finally there is dictionary comprehension. This works much the same, except that it has to take in a list of tuples. This works where one element in each tuple becomes the key and the other, the value. There are ways to make this work with a 1D list by splitting each element, however this is less of a beginner route.

The SUM function simply adds all the elements in a list together and outputs the result.

The REPLACE function is used to replace a substring inside of a string. The function also allows you to choose how many occurrences of the substring should be replaced. The default is to replace all occurrences.

The REVERSED and REVERSE functions are slightly different but do the same thing. The both reverse or flip an input list. The only difference is that REVERSED returns a reversed version of an input list while REVERSE is called on a list and reverses it, returning nothing.

Similarly, strings can be reverse by simply adding “[::-1]” after the variable name.

Hopefully all of these tips and tricks will make you more mindful of when you can code more efficiently and will help you think of more creative solutions to your coding problems.

Originally published at https://kgdavidson.co.uk on February 22, 2021.

--

--

ViscousPotential
Analytics Vidhya

A developer and electronics enthusiast wanting to share his experiences