1. dict comprehension

>>> for s in portfolio:
        holdings[s['name']] += s['shares']

>>> holdings
{ 'AA': 100, 'GE': 95, 'IBM': 150, 'MSFT':250, 'CAT': 150 }
>>>
prices 딕셔너리의 항목 중 포트폴리오에 포함된 종목만 추려보자.

>>> portfolio_prices = { name: prices[name] for name in names }
>>> portfolio_prices
{'AA': 9.22, 'GE': 13.48, 'IBM': 106.28, 'MSFT': 20.89, 'CAT': 35.46}

2. Generator

리스트 컴프리헨션 vs 제너레이터

before = [1,2,3,4,5]
after = [v*2 for v in before if v % 2 ==0]
before = [1,2,3,4,5]
after_gene = (v*2 for v in before if v % 2 == 0)
print(after_gene)

# 호출 할 때마다 실행