Post #1350657
2026-04-14 17:43 UTC
Python Tip #104 (of 365):
Use an _ to represent a placeholder variable.
This can be handy when you need to use range to loop N times but you don't care about the current iteration number in the loop:
matrix = [[0] * 3 for _ in range(3)]
Or when you're unpacking a tuple and one or more tuple values are unimportant:
[(item, _)] = collections.Counter(items).most_common(1)
When I see _ I think "I don't need to care about that value".
#Python #DailyPythonTip
Replies (0)
No replies.