[자료구조] Dictionary의 del과 pop() 함수

신은지·2024년 8월 3일

Data Structure

목록 보기
30/31
post-thumbnail

del

del과 key를 이용한 item 삭제


pop()

pop()과 key를 이용한 item 삭제


💡Python으로 딕셔너리 삭제하기

# Dictionary에 저장된 점수 중 최저 및 최고 점수를 삭제
scores = {'save1' : 8.9, 'save2' : 8.1, 'save3' : 8.5, 'save4' : 9.8, 'save5': 8.9}
minScore = 10; minKey = ''
maxScore = 0; maxKey = ''
print(scores)

for key in scores.keys():
    if scores[key] < minScore:
        minScore = scores[key]
        minKey = key

    if scores[key] > maxScore:
        maxScore = scores[key]
        maxKey = key

print('최저 점수는 {} {}점'.format(minKey, scores.get(minKey)))
print('최고 점수는 {} {}점'.format(maxKey, scores.get(maxKey)))
del scores[minKey]
del scores[maxKey]
print(scores)





* 이 글은 제로베이스 데이터 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.

profile
I believe there is no best, only better

0개의 댓글