파이썬에서는 리스트 컴프리헨션 기능을 제공하는 문법이 있다. 이번 장에서는 리스트 컴프리헨션 기능과 관련된 문법을 정리해보도록 하자.

리스트 컴프리헨션

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares = [x**2 for x in a]
print(squares)

>>>
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

파이썬 내장함수 map

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares = map(lambda x: x ** 2, a)

조건식 추가

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_squares = [x**2 for x in a if x % 2 == 0]
print(even_squares)

>>>
[4, 16, 36, 64, 100]
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10
alt = map(lambda x: x**2, filter(lambda x: x % 2 == a))

딕셔너리/세트 컴프리헨션

chile_ranks = {"ghost": 1, "habanero": 2, "cayenne": 3}
rank_dict = {rank: name for name, rank in chile_ranks.items()}
chile_len_set = {len(name) for name in rank_dict.values()}
print(rank_dict)
print(chile_len_set)

>>>
{1: "ghost", 2: "habanero", 3: "cayenne"}
{8, 5, 7}