데이터 순회하기

임혜림·2022년 11월 28일
0

변할 수 없는 값만이 key가 될 수 있다. ex) 튜플

즉, list는 key가 될 수 없다.

딕셔너리의 키 확인하기
accounts = {"kdhong":"Kildong Hong", }

print("kdhong" in accounts) # True
print("elice" in accounts) # False
딕셔너리 순회하기
accounts = {"kdhong":"Kildong Hong", }
for username, name in accounts.items():
	print(username + " - " + name)

실습

# 사용자가 시청한 작품의 리스트를 저장합니다. 수정하지 마세요. 
user_to_titles = {
    1: [271, 318, 491],
    2: [318, 19, 2980, 475],
    3: [475],
    4: [271, 318, 491, 2980, 19, 318, 475],
    5: [882, 91, 2980, 557, 35],
}
def get_user_to_num_titles(user_to_titles):
    user_to_num_titles = {}
    
    # 아래 함수를 완성하세요.
    for user, num in user_to_titles.items():
        user_to_num_titles[user] = len(num)

    return user_to_num_titles
    

# 아래 주석을 해제하고 결과를 확인해보세요.  
print(get_user_to_num_titles(user_to_titles))

0개의 댓글