What are list comprehensions?

Python control flow and functions · Freshers and experienced

Model answer

A compact way to build a list: [x * 2 for x in nums if x > 0]. Similar syntax exists for sets, dictionaries and generator expressions. They are clear for simple transformations. If a comprehension gets long or nested, a normal loop is more readable.

Learn this topic

Functions in Python: def, Arguments and return in the course Python for Beginners: Learn to Code with Real Projects.

Sign in to add your own answer.

More questions