TIL(22.12.14) - Python list내 dictionary 중복 제거

이지영·2022년 12월 14일
0

TIL/WIL

목록 보기
90/110

중복된 딕셔너리 제거

  • id 중복제거 -> id가 3인 계정 하나만 출력
accounts = [
	{'id': 1, 'username':'kim'},
    {'id': 2, 'username':'lee'},
    {'id': 3, 'username':'park'},
    {'id': 3, 'username':'choi'},
]

x = list({account['id']: account for account in accounts}.values())

# 결과
[{'id': 1, 'username':'kim'}, {'id': 2, 'username':'lee'}, {'id': 3, 'username':'park'},]

- username 중복제거 -> 모든 리스트 출력

accounts = [
	{'id': 1, 'username':'kim'},
    {'id': 2, 'username':'lee'},
    {'id': 3, 'username':'park'},
    {'id': 3, 'username':'choi'},
]

x = list({account['username']: account for account in accounts}.values())

# 결과
[{'id': 1, 'username':'kim'}, {'id': 2, 'username':'lee'}, {'id': 3, 'username':'park'},{'id': 3, 'username':'choi'},]
profile
🐶🦶📏

0개의 댓글