[Python] Comprehension Syntax
A function that simplifies working with lists, dictionary key/value pairs, and sets. \ In Python, you can use a special syntax called a comprehension to iterate
GGAMI
A function that simplifies working with lists, dictionary key/value pairs, and sets. \ In Python, you can use a special syntax called a comprehension to iterate
Contents
A function that simplifies working with lists, dictionary key/value pairs, and sets.
In Python, you can use a special syntax called a comprehension to iterate concisely over the types above and create data structures.
a = [1,2,3,4,5,6,7,8,9,10]
squares = []
for x in a:
sqaures.append(x**2)
print(squares)
>>> [1,4,9,16,25,36,49,64,81,100]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]a = [1,2,3,4,5,6,7,8,9,10]
even_sqaures = [x ** 2 for x in a if x % 2 == 0]
print(even_squares)
# 리스트의 원소가 제곱된 후 그 값이 나머지가 0이 되는 수
# 또 다른 조건을 추가하며 처리할 수 있다.
>>> [4,16,36,64,100]