Phương thức insert () List trong Python
Thí dụ
Chèn giá trị "màu cam" làm phần tử thứ hai của fruit
danh sách:
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
Định nghĩa và Cách sử dụng
Phương insert()
thức chèn giá trị đã chỉ định tại vị trí đã chỉ định.
Cú pháp
list.insert(pos, elmnt)
Giá trị tham số
Parameter | Description |
---|---|
pos | Required. A number specifying in which position to insert the value |
elmnt | Required. An element of any type (string, number, object etc.) |