มี 3 วิธีลบ object จาก list ใน Python: remove, del, pop
remove() ลบค่าแรกที่ตรงกับ argument ไใช่ index เฉพาะ
ตัวอย่าง
a = [3, 2, 3, 2]
a.remove(3)
print(a)
ผลลัพธ์
[2, 3, 2]
del ลบ index เฉพาะจาก list
ตัวอย่าง
a = [3, "Hello", 2, 1]
del a[1]
print(a)
ผลลัพธ์
[3, 2, 1]
pop() คืน element ที่ลบ argument คือ index ที่ต้องการลบ
ตัวอย่าง
a = [3, "Hello", 2, 1]
print(a.pop(1))
print(a)
ผลลัพธ์
"Hello"
[3, 2, 1]