Advanced Loop Techniques in Python: zip, range, filter, enumerate, and sorted
This article introduces several Python iteration tricks—including zip for parallel loops, C‑style range loops, filter for selective processing, enumerate for indexed iteration, and sorted for ordering data—providing concise code examples that make loops more efficient and readable.
For many beginner Python programmers, the for loop is the first concept they encounter, but over‑reliance on simple iteration can hide more efficient techniques.
Zip: Iterate over two lists simultaneously
Python makes it easy to loop over two sequences at once using the zip() function.
for first, second in zip(array1, array2):
print(first)
print(second)Example with an odd and an even number list:
odds = [1,3,5,7,9]
evens = [2,4,6,8,10]
for oddnum, evennum in zip(odds, evens):
print(oddnum)
print(evennum)The output is the numbers 1 through 10 in order.
In‑range function: C‑style loop
A C‑style loop can be written with range():
for i in range(10):
print(i)
if i == 3:
i.update(7)Although the snippet is not a true C‑style loop, it demonstrates how Python can emulate that pattern.
Custom C‑style iterator class
The author also provides a custom iterator class to mimic a more flexible C‑style loop (code omitted for brevity).
filter() function: Loop only needed data filter() lets you iterate over items that satisfy a condition, avoiding extra checks inside the loop.
people = [{"name": "John", "id": 1}, {"name": "Mike", "id": 4}, {"name": "Sandra", "id": 2}, {"name": "Jennifer", "id": 3}]
for person in filter(lambda i: i["id"] % 2 == 0, people):
print(person)This prints the entries with even id values.
enumerate() function: Indexing dimensions enumerate() adds an index to each element during iteration.
l = [5, 10, 15]
for idx, value in enumerate(l):
print(idx, value)The indices and values are combined into tuples automatically.
sorted() function: Ordering data sorted() returns a new list sorted in ascending order; setting reverse=True sorts descending, and a key function can customize ordering.
l = [15, 6, 1, 8]
for i in sorted(l):
print(i)
# Output: 1 6 8 15
for i in sorted(l, reverse=True):
print(i)
# Output: 15 8 6 1
l.sort(key=lambda s: s[::-1])These techniques help keep Python code concise, clear, and performant, encouraging deeper exploration of the language's powerful iteration capabilities.
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.
