OnePythonTip
101 posts

OnePythonTip
@OnePythonTip
Daily bite-sized #Python & #DataScience gems. Maker of cool tees for devs. #MachineLearning #DataAnalytics #Programming #Coding #WebDeveloper
参加日 Temmuz 2023
22 フォロー中1 フォロワー

Stop using range(len()) to loop through lists.
❌ Ugly:
for i in range(len(list)):
print(i, list[i])
✅ Clean:
for i, item in enumerate(list):
print(i, item)
enumerate() gives you index AND value in one line.
One tip a day. Follow for more.
#Python #LearnToCode 🐍

English

Counter is a dictionary for counting things.
Manual way:
counts = {}
for item in items:
counts[item] = counts.get(item, 0) + 1
Counter way:
from collections import Counter
counts = Counter(items)
One tip a day. Follow for more.
#Python #DataScience 🐍

English

Flatten nested lists with chain.from_iterable().
Nested = [[1,2], [3,4], [5,6]]
chain.from_iterable (nested)
→ [1,2,3,4,5,6]
Why better than manual loops:
- Faster (C implementation)
- Cleaner
- Memory efficient
One tip a day. Follow for more.
#Python #DataScience 🐍

English

@cached_property computes value ONCE, then caches it. Good for expensive ops.
Example:
class Report:
@cached_property
def sales_data(self):
return database.query("SELECT * FROM sales") # once
One tip a day. Follow for more.
#Python #DataScience 🐍

English

partial creates a new function with some arguments pre-filled.
With partial:
square = partial(power, exponent=2)
Perfect for:
- Specializing general functions
- Callbacks with fixed parameters
- Reducing repetition
One tip a day. Follow for more.
#Python #DataScience 🐍

English

💡 My "pro" moments:
1. Using enumerate() instead of range(len())
2. List comprehensions
3. defaultdict for grouping
4. Writing my first decorator (felt like magic)
What was YOUR moment? Be honest.
#Python #LearnToCode 🐍

English

f-strings can format numbers, dates, and alignment.
Numbers:
{value:.2f} # 2 decimals
{value:,} # commas
{value:.1%} # percentage
Dates:
{now:%Y-%m-%d} # 2026-06-14
Alignment:
{name:<10} # left align
One tip a day. Follow for more.
#Python #DataScience 🐍

English

sorted() with key lets you define custom sorting rules.
Default: sorts by value
Examples:
words = ["cat", "elephant", "dog"]
sorted(words, key=len) # ['cat', 'dog', 'elephant']
One tip a day. Follow for more.
#Python #LearnToCode 🐍

English









