Five Essential Python Tricks for Efficient Coding
This article introduces five practical Python tricks—including string manipulation, list comprehensions, lambda and map functions, single-line conditional expressions, and the zip function—providing concise examples and code snippets to help developers write cleaner, more efficient code.
Python is a powerful and easy-to-learn language with concise syntax, and this guide presents five commonly used Python tricks to make code shorter and more expressive.
1. String Operations
Python can use arithmetic operators such as + for concatenation and * for repetition to manipulate strings.
+ concatenates strings.
* repeats a string.
<code>my_string = "Hi Python..!"
print(my_string * 2) # Hi Python..!Hi Python..!
print(my_string + " I love Python" * 2) # Hi Python..! I love Python I love Python</code>You can also reverse a string (or any sequence) with slicing [::-1] :
<code>my_string = "Hi Python..!"
print(my_string[::-1]) # !..nohtyP iH
my_list = [1,2,3,4,5]
print(my_list[::-1]) # [5, 4, 3, 2, 1]
word_list = ["awesome", "is", "this"]
print(' '.join(word_list[::-1]) + '!') # this is awesome!</code>2. List Comprehensions
List comprehensions provide a compact, readable way to create new lists. For example, applying a function to all odd numbers in a list:
<code>def stupid_func(x):
return x**2 + 5
my_list = [1, 2, 3, 4, 5]
# Traditional loop
new_list = []
for x in my_list:
if x % 2 != 0:
new_list.append(stupid_func(x))
print(new_list) # [6, 14, 30]
# List comprehension
print([stupid_func(x) for x in my_list if x % 2 != 0]) # [6, 14, 30]
# Even more concise without a named function
print([x**2 + 5 for x in my_list if x % 2 != 0]) # [6, 14, 30]</code>3. Lambda & Map Functions
Lambda
A lambda is a small anonymous function useful for simple operations without defining a full function.
<code>stupid_func = (lambda x: x**2 + 5)
print([stupid_func(1), stupid_func(3), stupid_func(5)]) # [6, 14, 30]
# Using lambda as a key for sorting by square value
my_list = [2, 1, 0, -1, -2]
print(sorted(my_list, key=lambda x: x**2)) # [0, -1, 1, -2, 2]</code>Map
The built‑in map applies a function to each element of a sequence. Combined with a lambda, it can multiply elements of two lists element‑wise:
<code>print(list(map(lambda x, y: x * y, [1, 2, 3], [4, 5, 6]))) # [4, 10, 18]
# Equivalent explicit loop
x, y = [1, 2, 3], [4, 5, 6]
z = []
for i in range(len(x)):
z.append(x[i] * y[i])
print(z) # [4, 10, 18]</code>4. Single‑Line If‑Else Expression
Complex conditional logic can be reduced to a single expression using the ternary operator:
<code>x = int(input())
print("Horse" if x >= 10 else "Duck" if 1 < x < 10 else "Baguette")</code>5. zip() Function
zip() pairs elements from multiple iterables. It can be used to combine first and last names:
<code>first_names = ["Peter", "Christian", "Klaus"]
last_names = ["Jensen", "Smith", "Nistrup"]
print([" ".join(x) for x in zip(first_names, last_names)])
# ['Peter Jensen', 'Christian Smith', 'Klaus Nistrup']</code>The five quick tricks above aim to help you write more concise and readable Python code.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.