Skip to content
Python

리스트 컴프리헨션

리스트 컴프리헨션을 사용해 리스트를 빠르게 생성.

#list#comprehension

Code

python
# Generate list of squares
squares = [x**2 for x in range(10)]
print(squares)

# Conditional comprehension
evens = [x for x in range(20) if x % 2 == 0]

# Nested comprehension
matrix = [[i * j for j in range(3)] for i in range(3)]

# Dict comprehension
word_len = {w: len(w) for w in ["hello", "world", "python"]}