본문 바로가기

Python

[Python] lambda vs list comprehension

아래 두개는 동일한 표현식 결과도 같다. 

range(1,10)을 쓰면  [1,2,3,4,5,6,7,8,9]의 list를 반환한다.  

요즘 추세는 list comprehension을 쓰는 trend. lambda를 쓰면 readability가 떨어진다고....


1) Lambda

>>> list(filter(lambda x:x%3==0, range(1,10)))

[3, 6, 9]



2) list comprehension

>>> [x for x in range(1,10) if x%3 == 0]

[3, 6, 9]


'Python' 카테고리의 다른 글

Formatting time  (0) 2022.07.15
List with *  (0) 2020.08.12
[Python] Virtual Environment  (0) 2019.01.09
Xing API] Real time data subscribe  (0) 2016.03.03
Xing API] Connection and login  (0) 2016.03.02