รายการ Python ขยาย ()วิธีการ
ตัวอย่าง
เพิ่มองค์ประกอบของcars
รายการfruits
:
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
ความหมายและการใช้งาน
วิธี การextend()
เพิ่มองค์ประกอบรายการที่ระบุ (หรือแบบวนซ้ำได้) ที่ส่วนท้ายของรายการปัจจุบัน
ไวยากรณ์
list.extend(iterable)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
iterable | Required. Any iterable (list, set, tuple, etc.) |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
เพิ่มทูเพิลในfruits
รายการ:
fruits = ['apple', 'banana', 'cherry']
points = (1, 4, 5, 9)
fruits.extend(points)