사전은 일상 프로그래밍, 웹 개발 및 AI/ML 프로그래밍과 같은 다양한 실용적인 응용 프로그램에서도 사용되므로 전체적으로 유용한 컨테이너입니다. 따라서 사전 사용과 관련된 다양한 작업을 수행하는 방법을 아는 것은 항상 장점입니다.
예시
# using del # Initializing dictionary test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25} # Printing dictionary before removal print ("The dictionary before performing remove is : " + str(test_dict)) # Using del to remove a dict del test_dict['Vishal'] # Printing dictionary after removal print ("The dictionary after remove is : " + str(test_dict)) # using pop() # Initializing dictionary test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25} # Printing dictionary before removal print ("The dictionary before performing remove is : " + str(test_dict)) # Using pop() to remove a dict. pair removed_value = test_dict.pop('Ram') # Printing dictionary after removal print ("The dictionary after remove is : " + str(test_dict)) print ("The removed key's value is : " + str(removed_value)) # Using pop() to remove a dict. pair doesn't raise exception # assigns 'No Key found' to removed_value removed_value = test_dict.pop('Nilesh', 'No Key found') # Printing dictionary after removal print ("The dictionary after remove is : " + str(test_dict)) print ("The removed key's value is : " + str(removed_value)) # using items() + dict comprehension # Initializing dictionary test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25} # Printing dictionary before removal print ("The dictionary before performing remove is : " + str(test_dict)) # Using items() + dict comprehension to remove a dict. pair new_dict = {key:val for key, val in test_dict.items() if key != 'Prashant} # Printing dictionary after removal print ("The dictionary after remove is : " + str(new_dict)) '