Journey Toward Writing Better Python Code!!
25 Amazing Python Tricks That Will Instantly Improve Your Code
Get a sneak peek of top techniques used by top Developers to Write Clean, Efficient Python Code
I’ve been using Python for almost 5 years now, and one thing that still excites me is discovering new ways to improve my code. Over the past year, I’ve focused on refining my coding practices, and uncovering techniques and tricks that have significantly boosted my productivity and efficiency. In this blog, I’ll share some Python tricks that have made the most impact, helping to elevate the quality of your code instantly
1. Stop Wasting Time with x.append
—x.extend provides a much faster and cleaner way of extending lists
When adding several items to a list, opt for the extend()
method to efficiently append the entire iterable at once, instead of using append()
repeatedly for each element.
## Bad
data = [1,2,3]
data.append(4)
data.append(5)
data.append(6)
## Good
data = [1,2,3]
data.extend((4,5,6))