List Comprehension
List Comprehension is a reusable Python snippet. A concise way to create lists with Python's list comprehension syntax. More readable and faster than for loops. The full implementation: # Basic: squares of 0-9 squares = [x**2 for x in range(10)] # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] # With condition: even numbers only evens = [x for x in range(20) if x % 2 == 0] # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] # Nested: flatten a matrix matrix = [[1, 2], [3, 4], [5, 6]] flat = [num for row in matrix for num in row] # [1, 2,… It belongs to the Built-in Functions area of the standard library. This snippet uses only the Python standard library with no third-party dependencies, so it runs on any modern Python 3 interpreter. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.