DEV Community

niuniu
niuniu

Posted on

Quick Tip: Python List Comprehension

Quick Tip

Python list comprehension examples:

# Basic
squares = [x**2 for x in range(10)]

# With condition
evens = [x for x in range(10) if x % 2 == 0]

# Nested
matrix = [[1,2,3],[4,5,6],[7,8,9]]
flat = [x for row in matrix for x in row]
Enter fullscreen mode Exit fullscreen mode

Powered by MonkeyCode: https://monkeycode-ai.net/

python #coding #tips

Top comments (0)