Fundamentals 10 min read

Python Advanced Features: List Comprehensions, Iterators, and Generators

This article introduces Python's advanced features—list comprehensions, dictionary and set comprehensions, iterators, and generators—explaining their syntax, providing interview‑style examples, and showing how they improve code readability, efficiency, and memory usage.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Advanced Features: List Comprehensions, Iterators, and Generators

The article explains three core Python features that are frequently asked in interviews: list comprehensions, iterators, and generators, and shows how they relate to each other.

List Comprehensions

List comprehensions create a new list from an existing one using the syntax [expression for variable in old_list] or with a conditional [expression for variable in old_list if condition]. Example:

names_old = ['tom', 'amy', 'daming', 'lingling']
names_new = [name.capitalize() for name in names_old if len(name) > 3]
print(names_new)  # ['Daming', 'Lingling']

Other examples include generating tuples of even‑odd pairs and updating employee salary dictionaries using conditional expressions inside a comprehension.

Dictionary and Set Comprehensions

Dictionary comprehensions swap keys and values: {value: key for key, value in dict_old.items()}. Set comprehensions remove duplicates: {x for x in list_old}.

Iterators

An iterator is an object that implements the __next__() method and can be advanced with next(). Objects that implement the iterator protocol are called iterables. Files, lists (via iter()), and generators are examples.

with open('F:/test/test.txt') as f:
    for line in f:
        print(line)

Use isinstance(obj, Iterable) to test iterability.

Generators

Generators produce items lazily, saving memory. They can be created via generator expressions or generator functions using yield. Example of a generator expression:

my_generator = (x for x in range(5))
print(type(my_generator))  # <class 'generator'>
print(next(my_generator))   # 0
for i in my_generator:
    print(i)

Example of a generator function (Fibonacci):

def fib(length):
    a, b = 0, 1
    n = 0
    while n < length:
        n += 1
        yield b
        a, b = b, a + b

g = fib(5)
print(next(g))  # 1
print(next(g))  # 1
print(next(g))  # 2

Generators can be iterated only once; each next() resumes execution from the last yield point.

Conclusion

Using comprehensions and generators makes Python code more concise, readable, and memory‑efficient. Mastering these constructs is essential for writing Pythonic, high‑performance programs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonIteratorgeneratorlist-comprehension
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.