Python One-Liner Codes- Part 1

This language known as python has become widely used among developers. Many of them are shifting their career to this language because it is easy to use and can make you abbreviate an extended code into one line code known as a one-liner code in python. 

It also has some tricks that you can implement to make your job much easier, and this article will help you learn some of the most used codes among developers and how you use this one-liner in python code.

1. List Comprehension

List comprehension is a very straightforward method of creating a list from another list or adding items without using the for loops, making it much easier than the traditional way. 

So it is considered much faster also compared to using the for loos, and let’s see an example of adding items in a conventional way than moving to list comprehension:

data = []

for x in range (6):
    data.append(x)

print(data)

You can see that we’ve declared an empty list, and the for loop will loop over the numbers generated by the range() function from 0 to 5 and add them to the empty list. This syntax is correct, but you can make it easier than these lines of code. Let’s see an example:

data = [x for x in range(6)]

print(data)

It will add the variable x which is the numbers generated by the range() function to the list directly without using the .append() method like in the previous example. This way, you can produce the same results with just one line of code instead of three, and looks more professional.

2. List Comprehension With If Condition

The previous code shows you adding items to the list using the for loops and range() function but without checking for a condition. You need to use the if statement to verify a condition in some situations before adding it to the list. Let’s see how you can perform this action in a conventional way than using the list comprehension:

data = []

for x in range (10):
    if x%2 == 1:
        data.append(x)

print(data)

After verifying the if statement’s condition, the code will add only the odd numbers to the list. We can do the same action using the list comprehension:

data = [x for x in range(10) if x%2 == 1]

print(data)

You can see that it prints the same result with only one line of code and checks if the numbers are odd before adding them to the list.

3. One If-Else Condition

Most of you know that we use the If statement to evaluate a condition, and if it meets, it executes the code inside it. We also use the else if the condition didn’t meet in the if so, the python interpreter will execute the code inside the else statement. Let’s see a simple example:

x = 86
y = 61

if x > y:
    print("x is greater than y")
else:
    print("y is greater than x")

The previous code will print the first statement since it is evaluated as True and it will ignore the code inside the else statement. You see that it has many lines of code, and you can look more professional by using this trick in python:

x = 86
y = 61

print("x is greater than y") if x > y else print("y is greater than x")

This code looks more professional than the previous one, and it will print the first statement since the if statement is evaluated to True. Otherwise, it will execute the second one.

4. Many If-Else Conditions

In the previous example, we’ve only used one if-else statement. If you are an excellent python developer, you know that if we need to use another condition to check, we will use the elif statement, which is a combination of the if-else statements. Let’s first see a simple example of this, and then we will see how you can implement the same in one line of code:

x = 180

if x < 67:
    print("x is less than 67")
elif x == 180:
    print("x is equal to 180")
else:
    print("x is above 67")

The code above will print the second option which is related to the elif statement since the x is equivalent to 180 which is just a random number I’ve chosen for this example. Let’s see now how you can implement the same using the one-line code:

x = 180

print("x is less than 67") if x < 67 else print("x is equal to 180") if x == 180 else print("x is above 67")

You can see that it will produce the same results. You can see that it is very similar to the previous example but with one change which is that the elif statement is broken into two separated sections:

else print("x is equal to 180") if x == 180

5. Assign Multiple Values

When you are an excellent python developer and start creating large codes, you will probably need to initiate and use many variables in your code, so maybe it will be a daunting task to make them one by one. You can assign many values to many variables in the same line. Let’s see an example:

Germany, USA, Russia = "Berlin", "Washington", "Moscow"

print(Germany, USA, Russia)

You can see that it worked fine, and you are also assigned many values types such as integers and floats, not just strings like in this example.

Conclusion

Thanks for reading! I’ve shown you some of the best one-liner codes that will help you be more productive and make you look like a professional python developer. There are more one-liner codes than we’ve explored in this article, and I will discuss them in the following tutorial.

Scroll to Top