Python เพิ่มรายการ
เพิ่มรายการ
หากต้องการเพิ่มรายการต่อท้ายรายการ ให้ใช้append() วิธีการ:
ตัวอย่าง
ใช้append()
วิธีการต่อท้ายรายการ:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
ในการเพิ่มรายการที่ดัชนีที่ระบุ ให้ใช้ เมธอด insert() :
ตัวอย่าง
แทรกรายการเป็นตำแหน่งที่สอง:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)