[ (변수를 활용한 값) for (사용할 변수 이름) in (순회할 수 있는 값) ]
size = 10
arr = [i *2 for i in range(size)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
size = 10
arr = [n for n in range(1, 11) if n % 2 == 0]
[2, 4, 6, 8, 10]
arr = [n for n in range(1,31) if n%2 ==0 if n%3 ==0] # 가능
arr = [n for n in range(1, 31) if n%2 == 0 and n%3 ==0] # 가능
arr = [n for n in range(1, 31) if n%2 == 0 or n%3 ==0] # 가능
arr = [n for n range(1, 31) if n %2 == 0 and if n^3 ==0] # 불가능
arr = [n for n range(1, 31) if n %2 == 0 or if n^3 ==0] # 불가능
arr = [[1,2,3], [4,5,6], [7,8,9], [10,11,12],]
# 2차원 -> 1차원 만들기
flat_one = [n for row in arr for n in row]
[1,2,3,4,5,6,7,8,9,10,11,12]
# 차원 2차원으로 유지하면서 원소 제곱하기
squared_list = [[n**2 for n in row] for row in arr]
[[1, 4, 9] , … , [100,121, 144]]
—————————————-
found = {name:batches for name in order if (batches:=get_batches(stock.get(name, 0), 8))}
{}
) comprehensionset_boy = {n ** 2 for n in range(20)}
{0, 1, 64, 4, 36, 9, 16, 49, 81, 25}
{key: value, …}
) comprehensionfrom string import ascii_lowercase as LOWERS
dict_boy = {c: n for c, n in zip(LOWERS, range(1, 27) ) }
{‘a’ : 1 , ‘b’: 2, ‘c’ : 3, … , ‘x’: 24, ‘y’: 25, ‘z’: 26}
print(dict(zip(LOWERS, range(1, len(LOWERS)+1))))
tuple_boy = tuple(n for n in range(1, 10))
generator_boy = (n for n in range(1, 10))
<generator object <genexpr> at ~~~>