Python One-Liner Codes- Part 2

Python one-liner code is just a simple code that you can write in just one line, making your code look much better and more professional. Also, it makes you more productive by saving time and understanding your code better for you or for other people working with you in the IT department if you are in a team developing powerful software. We’ve talked about one-liner codes in the previous tutorial, and we will see more about them in this article.

1. Swapping Elements In The List

You know that the elements in the list can be changed, like removing it or changing its values. In some situations, you need to swap the list elements like the first elements will be the last one, and the last one will be the first one. This can be done quickly in python with a straightforward command:

capitals = ["Berlin", "Washington", "Moscow"]
print(capitals)

capitals[1], capitals[2] = capitals[2], capitals[1]
print(capitals)

You will see the differences after printing the list the first time and changing the elements. The second element will be swapped with the third one, and remember that counting starts from zero, not one.

2. Lambda Function

The Lambda function in python is like the regular function you use in your python code, except it doesn’t have a name, and also it allows only one expression. 

Also, it can have multiple arguments with one expression. Let’s first see how you can make a standard python function, and we will see how you can make the same with lambda in one line of code:

def mul(x):
    return x * 2

print(mul(5))

The function is doing multiplication for the number five times two, and we needed two lines of code. Let’s now see the lambda function:

mul = lambda x: x * 2

print(mul(5))

The lambda function has a variable name called mul which will be used when you call it. Then you will declare a lambda function using the lambda function and pass the x, which is the variable like what you see inside the parenthesis in the normal function mul(x) and followed by the expression.

3. Reverse a List

You can reverse a list in python in many ways, and if you know python very well, you could get creative with many different codes to make this. Let’s see how you can use the reverse() function to reverse a list:

def rev(numbers):
    return [elements for elements in reversed(numbers)]
     
numbers = [20, 21, 22, 23, 24, 25]

print(numbers)

print(rev(numbers))

The code above uses the reversed() list method to reverse the numbers inside a list. You can use any elements in the list, such as strings or float numbers. Let’s now see how you can perform the same action using one line of code:

numbers = [20, 21, 22, 23, 24, 25]
print(numbers)

reversed_numbers = numbers[::-1]
print(reversed_numbers)

You will get the same result with only one line of code and without using any built-in list method. You can convert the code to a function that you will use in your program:

def rev(numbers):
    return numbers[::-1]

numbers = [20, 21, 22, 23, 24, 25]

print(numbers)

print(rev(numbers))

4. Read File And Input It To a List

Sometimes you need to get content from a text file and print it on the screen or use it in your python code. We will create a simple python code that will open a .txt file, read its content, and add them to a list. Let’s see how we can perform this action:

capitals = open("capitals.txt", "r")

List = []

for line in capitals:
    x = line[:-1]
    List.append(x)

capitals.close()

print(List)

First, you need to read the file in the read more. Then you will loop over these elements inside the .txt file and remove the extra lines using the line[:-1]. 

Later, add them to the empty list we’ve already declared using the .append() function. Finally, close the file with the .close() function and print the list on the screen. It contains many lines of code, but you can make the same using this one line of code:

List = [line.strip() for line in open('capitals.txt', 'r')]

print(List)

You will have the same result with only one line of code, and it looks more professional and easy to understand.

5. Or Boolean With If condition

You will often need to use the if statement to check for the conditions before executing the code. There are many boolean types to check, but we are talking about the Or boolean, which will run the code if one condition is met. Let’s see an example:

x = 10

if x == 3 or x == 5 or x == 10:
    print("Condition Met")

The code is working perfectly, but what if we need to check many other conditions, making it a daunting task to write that code. Let me help you by using a simple one code and trick that will make it so easy for you:

x = 10

if x in [3, 5, 10]:
    print("Condition Met")

The code above will make the same result as the previous one, and if the value x is inside the closed brackets, it will be evaluated as True, which will execute the print function.

Conclusion

Thanks for reading! I’ve gone through some of the examples about python one-liner codes that make it easy to code with python and make your code looks more professional. There is a lot more python one-liner code in this language, we didn’t explore all of them, and we definitely will in the following tutorials.

Scroll to Top