10 Hidden Python Features You Probably Don’t Know (2025 Edition)
10 Hidden Python Features You Probably Don’t Know (2025 Edition)
Python is simple on the surface — but deep inside, it has dozens of underrated features that even intermediate developers often overlook. These hidden gems can make your code faster, cleaner, and more Pythonic.
Here are 10 powerful Python features you probably don’t know (but should!).
1. The any() and all() Functions
Great for quick validations and conditions.
any([False, False, True]) # True
all([True, True, True]) # True
2. Use get() to Avoid Dictionary Errors
Returns a default value instead of KeyError.
value = my_dict.get("age", "Not Found")
3. The setdefault() Trick
user = {}
user.setdefault("name", "Sachin")
print(user)
4. Tuple Unpacking With *
a, *middle, b = [1, 2, 3, 4, 5]
print(a, middle, b)
5. The for…else Feature
Runs else only if loop does NOT break.
for num in numbers:
if num == 10:
print("Found")
break
else:
print("10 not found")
6. Unpacking Dictionary Using **kwargs
def greet(name, age):
print(name, age)
info = {"name": "Sachin", "age": 26}
greet(**info)
7. Use enumerate() Instead of range(len())
languages = ["Python", "Java", "C++"]
for index, lang in enumerate(languages, start=1):
print(index, lang)
8. Sort Without Lambda Using itemgetter()
from operator import itemgetter
students = [
{"name": "Raj", "score": 90},
{"name": "Avi", "score": 85}
]
sorted_students = sorted(students, key=itemgetter("score"))
print(sorted_students)
9. Dictionary Comprehension
pairs = [("a", 1), ("b", 2)]
result = {k: v*2 for k, v in pairs}
print(result)
10. Using _ for Throwaway Values
for _ in range(3):
print("Hello")
Bonus: Pretty Print JSON
import json
data = {"name": "Sachin", "language": "Python"}
print(json.dumps(data, indent=4))
Conclusion
Python is full of hidden gems that can improve your code quality and save you time. Try using one or two of these features in your next project and immediately feel the improvement.
Comments
Post a Comment