เมื่อจำเป็นต้องลบพจนานุกรมออกจากรายการพจนานุกรมเมื่อไม่มีค่าใดค่าหนึ่งอยู่ในนั้น ระบบจะใช้การทำซ้ำอย่างง่าย ตัวดำเนินการ 'del' และคำสั่ง 'break'
ตัวอย่าง
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
my_list = [{"code" : 1, "fun" : "learn"},
{"code" : 2, "fun" : "code"},
{"code" : 3, "fun" : "test"},
{"code" : 4, "fun" : "teach"},
{"code" : 4, "fun" : "make"},
{"code" : 4, "fun" : "object"}]
print("The list of dictionary is : " )
print(my_list)
for index in range(len(my_list)):
if my_list[index]['code'] == 3:
del my_list[index]
break
print("The resultant list is : ")
print(my_list) ผลลัพธ์
The list of dictionary is :
[{'code': 1, 'fun': 'learn'}, {'code': 2, 'fun': 'code'}, {'code': 3, 'fun': 'test'}, {'code': 4, 'fun': 'teach'},
{'code': 4, 'fun': 'make'}, {'code': 4, 'fun': 'object'}]
The resultant list is :
[{'code': 1, 'fun': 'learn'}, {'code': 2, 'fun': 'code'}, {'code': 4, 'fun': 'teach'}, {'code': 4, 'fun': 'make'},
{'code': 4, 'fun': 'object'}] คำอธิบาย
-
รายการสตริงถูกกำหนดและแสดงบนคอนโซล
-
รายการถูกทำซ้ำ และดัชนีเฉพาะจะถูกตรวจสอบให้เท่ากับจำนวนเต็ม
-
สิ่งนี้ถูกกำหนดให้กับผลลัพธ์
-
สิ่งนี้จะแสดงเป็นเอาต์พุตบนคอนโซล