Python - ลบรายการรายการ
ลบรายการที่ระบุ
วิธี การremove()
ลบรายการที่ระบุ
ตัวอย่าง
ลบ "กล้วย":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
ลบ Specified Index
วิธี การpop()
ลบดัชนีที่ระบุ
ตัวอย่าง
ลบรายการที่สอง:
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
หากคุณไม่ได้ระบุดัชนีpop()
วิธีการจะลบรายการสุดท้าย
ตัวอย่าง
ลบรายการสุดท้าย:
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
del
คีย์เวิร์ดยังลบดัชนีที่ระบุ:
ตัวอย่าง
ลบรายการแรก:
thislist = ["apple", "banana", "cherry"]
del
thislist[0]
print(thislist)
คีย์เวิร์ด ยังdel
สามารถลบรายการทั้งหมดได้
ตัวอย่าง
ลบรายการทั้งหมด:
thislist = ["apple", "banana", "cherry"]
del
thislist
ล้างรายการ
วิธี การclear()
ล้างรายการ
รายการยังคงอยู่ แต่ไม่มีเนื้อหา
ตัวอย่าง
ล้างเนื้อหารายการ:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)