What is a generator expression and how does it differ from a list comprehension?
Model answer
A generator expression, (x * x for x in nums), produces items lazily one at a time and uses almost no memory, while a list comprehension builds the entire list. Use generators for large or streaming data, and when you only need to iterate once, such as sum(x for x in nums).
Learn this topic
Dictionaries, Tuples and Sets in Python in the course Python for Beginners: Learn to Code with Real Projects.
Sign in to add your own answer.
More questions
- How do you add and remove items from a list?
- What is a dictionary and how do you use it?
- What is a set and when would you use one?
- How do you remove duplicates from a list while keeping order?
- How do you sort a list of dictionaries by a key?
- What is the time complexity of common list, dict and set operations?