Back-end Summary

python 에서 list 를 dict 으로 변환하는 법

고수트 2021. 1. 31. 20:49
반응형

파이썬을 하다보면 리스트 형식을 dict 형식으로 변환하고 싶을 때가 있다.

그럴 때에는 크게 두가지 방법이 있다.

dict.fromkeys 를 사용하는방법

unique_genres = ['Adventure', 'Action', 'Romance']

# dict.fromkeys 첫번째 파라미터에는 리스트, 두번째 파라미터에는 초기값을 적어준다.
genres_dict = dict.fromkeys(unique_genres, 0)

 

{} 기호를 이용하는 방법

unique_genres = ['Adventure', 'Action', 'Romance']

# dict.fromkeys 첫번째 파라미터에는 리스트, 두번째 파라미터에는 초기값을 적어준다.
genres_dict = {i:0 for i in unique_genres}

 

반응형