Today I Learn (TIL)
#9 Python - list comprehension
이웃집 친구
2020. 5. 27. 20:57
반응형
표현 방법들
[표현식 for 변수 in 반복(순회) 가능한 객체]
ex)
numbers = [1, 2, 3, 4, 5]
[i * 2 for i in numbers]
#[2, 4, 6, 8, 10]
[i for i in numbers if i % 2 ==0]
#[2, 4]
Well.. I really don't use those expressions that much tho; uhm.......maybe for the time will come.
for i in range(len(numbers)):
numbers[i] = int(numbers[i]) * 2
print(numbers)
#[2, 4, 6, 8, 10]
However, the list comprehension help your code shorter.