
เขียนโปรแกรมภาษา Python ตอนที่ 126 ลบข้อมูลใน Dictionary ด้วยเมธอด clear()
เราสามารถลบสมาชิกทั้งหมดใน Dictionary ได้โดยการใช้เมธอด clear()
โดยมีรูปแบบการใช้งานดังนี้
dictionary.clear()
โดยข้อมูลทั้งหมดใน Dictionary ต้นทางจะถูกลบออกไป
phone = { "model": "iPhone 14", "year": "2022", "price": "39,000" } print("Before removing") print(phone) phone.clear() print("After removing") print(phone)
ผลลัพธ์
Before removing
{‘model’: ‘iPhone 14’, ‘year’: ‘2022’, ‘price’: ‘39,000’}
After removing
{}