본문 바로가기
Engineering WIKI/Python

파이썬 리스트 컴프리헨션

by wonos 2022. 5. 4.
num_list = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

new_list = []

# 평소에는 아래와 같이 작성
for num in num_list:
    if num > 3:
        new_list.append(num)

# 여기서 List Comprehension을 사용하면 더 짧고 간결하게 코드를 작성할 수 있습니다.

new_list = [ num for num in num_list if num > 3 ]
# 이외에도 filter를 활용하는 방법도 있습니다.
new_list = list(filter(lambda x: x > 3, num_list))

'Engineering WIKI > Python' 카테고리의 다른 글

collections 모듈 - deque  (0) 2022.05.12
collections 모듈 - defaultdict  (0) 2022.05.12
collections 모듈 - Counter  (0) 2022.05.12
vars() 내장함수  (0) 2022.05.10
Python 데코레이터  (0) 2022.05.02
파이썬 __ slots __  (0) 2022.04.28
파이썬 property(), @property  (0) 2022.04.25
파이썬 비트 논리연산자  (0) 2022.04.21