Mind-Blowing Python Features You’re Not Using Enough!

Discover underrated Python features that can simplify your code, boost performance, and make you a more efficient developer! 🚀

Mind-Blowing Python Features You’re Not Using Enough!
Photo by Emmanuel Ikwuegbu on Unsplash

UNLOCK PYTHON’S HIDDEN GEMS

Mind-Blowing Python Features You’re Not Using Enough!

Hey Everyone, using python since long time and i realized python is packed with mind-blowing hidden features that probably we aren’t using enough.

But these hidden gems that can simplify your code, boost performance, and make you a more efficient developer.

In these guide we will explore those hidden mind-blowing python features.

1. Walrus Operator (:=) – Assign & Use in One Line

Python 3.8 introduced the walrus operator (:=), which allows assigning a value within an expression.

Example

if (n := len("Hello, Python!")) > 10: 
    print(f"String is long ({n} characters)")  # String is long (14 characters)
This Reduces redundancy and Makes loops and conditions cleaner.

2. f-strings with =, Formatting, and Debugging

Python’s f-strings (f"") are not just for inserting variables—they can also show variable names and apply formatting.

Example

name = "Alice" 
age = 25 
print(f"{name=}, {age=}")  # name='Alice', age=25 
print(f"{age:.2f}")  # 25.00 (formatted as float)
This Makes debugging easier and Allows inline formatting.

3. dataclass — No More Boilerplate Code!

The dataclass decorator (introduced in Python 3.7) automatically generates __init__, __repr__, and other methods for classes.

Example

from dataclasses import dataclass 
 
@dataclass 
class User: 
    name: str 
    age: int 
user = User("Alice", 25) 
print(user)  # User(name='Alice', age=25)
This Reduces repetitive class definitions and Provides immutability and defaults.

4. pathlib – Better Than os.path!

Stop using os.path for file handling—use pathlib for simpler and more readable code.

Example

from pathlib import Path 
 
path = Path("example.txt") 
if path.exists(): 
    print(path.read_text())  # Reads file content
It Works across Windows/Linux/Mac and Provides object-oriented file handling.

5. enumerate() – Stop Using range(len())!

The enumerate() function adds an index automatically while iterating.

Example

names = ["Alice", "Bob", "Charlie"] 
for i, name in enumerate(names, start=1): 
    print(f"{i}: {name}")   
 
# Output 
# 1: Alice 
# 2: Bob 
# 3: Charlie
It is Cleaner and more Pythonic and Avoids manual indexing.

6. collections.Counter – Easy Frequency Counting

Instead of manually counting occurrences, use Counter from the collections module.

from collections import Counter 
 
words = ["apple", "banana", "apple", "orange", "banana", "apple"] 
counter = Counter(words) 
print(counter.most_common(2))  # [('apple', 3), ('banana', 2)]
This Makes frequency calculations effortless and Supports most common operations (most_common(), subtract(), etc.)

7. zip() – Combine Iterables Like a Pro!

The zip() function pairs elements from multiple iterables into tuples.

Example

names = ["John", "David", "Sam"] 
ages = [25, 30, 35] 
 
for name, age in zip(names, ages): 
    print(f"{name} is {age} years old.") 
# Output 
# John is 25 years old. 
# David is 30 years old. 
# Sam is 35 years old.
This Reduces for loop complexity and Helps in parallel iteration.

8. itertools – Unlock Infinite Sequences & More

The itertools module contains powerful tools for working with iterators efficiently.

Example Infinite Counter:

from itertools import count 
 
for i in count(start=5, step=2): 
    if i > 15: 
        break 
    print(i)  # 5, 7, 9, 11, 13, 15

Example — Get All Pairs from a List:

from itertools import combinations 
 
items = ["A", "B", "C"] 
pairs = list(combinations(items, 2)) 
print(pairs)  # [('A', 'B'), ('A', 'C'), ('B', 'C')]
It Provides infinite iterators and Includes combinations, permutations, and more.

9. defaultdict – No More KeyErrors in Dicts!

The defaultdict automatically assigns a default value when a missing key is accessed.

Example

from collections import defaultdict 
 
scores = defaultdict(int)  # Default value is 0 
scores["Alice"] += 10 
print(scores["Alice"])  # 10 
print(scores["Bob"])  # 0 (No KeyError!)
This Avoids KeyErrors and Simplifies grouping operations.

10. functools.cache – Boost Performance with Caching!

The cache decorator speeds up function calls by storing results.

Example

from functools import cache 
 
@cache 
def fib(n): 
    if n < 2: 
        return n 
    return fib(n - 1) + fib(n - 2) 
print(fib(50))  # Much faster with caching!
This Improves performance and Ideal for expensive calculations.

Final Thoughts

These mind-blowing Python features can make your code cleaner, faster, and more powerful.

Are there any other hidden Python gems you use often? Let me know in the comments!