Python list insert() method
Insert an item at a position.
Syntax
list.insert(index, item)
Items from that position move one place to the right. insert(0, x) adds at the front.
Parameters
| index | the position |
| item | the value |
Return value
None
Example
Code
queue = ["Leo", "Mia"] queue.insert(0, "Sita") queue.insert(2, "Raj") print(queue)
Output
['Sita', 'Leo', 'Raj', 'Mia']
Good to know. Inserting at the front of a very long list is slow;
collections.deque is made for that.Related
Learn it step by step in the free lesson Lists & Tuples, with exercises that check your answers.