5 Python Idioms to Write Faster, More Memory‑Efficient Code
This article presents five practical Python idioms—range(), zip(), reversed(), enumerate(), and sorted()—explaining how to replace less efficient loops with built‑in functions to improve execution speed, readability, and memory usage, accompanied by clear code examples.
Python is praised for its simplicity and versatility, but writing idiomatic code that balances readability with execution speed and memory efficiency is essential for professional developers.
a. range() – Instead of manually listing numbers, use the built‑in range() function. It generates the sequence lazily and supports start, stop, and step arguments, making loops scalable from a few items to millions.
for i in [2,3,4,5,6,7,8,9,10]:
print(i)
for i in range(2, 11):
print(i)
for i in range(2, 11, 2):
print(i) # 2 4 6 8 10When iterating over a list, range(len(list)) can be replaced by direct iteration.
cloths = ['shirt', 'hat', 'socks', 'shorts']
for i in range(len(cloths)):
print(cloths[i])
for cloth in cloths:
print(cloth)b. zip() – Combine multiple iterables element‑wise. The built‑in zip() works with any number of sequences.
colours = ['pink', 'red', 'green']
cloths = ['shirt', 'hat', 'socks', 'shorts']
for colour, cloth in zip(colours, cloths):
print(colour, cloth)
# Output: pink shirt, red hat, green socksc. reversed() – Iterate a sequence backwards without manual index calculations.
cloths = ['shirt', 'hat', 'socks', 'shorts']
for cloth in reversed(cloths):
print(cloth)
# Output: shorts, socks, hat, shirtd. enumerate() – Access both index and element in a loop cleanly.
cloths = ['shirt', 'hat', 'socks', 'shorts']
for i, cloth in enumerate(cloths):
print(i, cloth)
# Output: 0 shirt, 1 hat, 2 socks, 3 shortse. sorted() – Iterate over a collection in sorted order without mutating the original. It uses TimSort (average O(n·log n)). The key argument allows custom sorting criteria, and reverse=True yields descending order.
nums = [2, 3, 1, 5, 2]
for i in sorted(nums):
print(i)
# Output: 1 2 2 3 5
cloths = ['shirt', 'hat', 'socks', 'shorts']
for cloth in sorted(cloths, key=len):
print(cloth)
# Output by length: hat, shirt, socks, shortsApplying these five built‑in functions helps you write Python code that is both idiomatic and performant.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
