English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The insert() method adds an element to the specified index in the list.
The syntax of the insert() method is
list.insert(index, element)
The insert() function takes two parameters:
index -The position where the element needs to be inserted
element -This is the element to be inserted into the list
The insert() method only inserts an element into the list. It does not return anything; it returns None.
# Vowel list vowel = ['a', 'e', 'i', 'u'] # At the4Insert element at a certain position vowel.insert(3, 'o') print('Updated list: ', vowel)
When the program is run, the output is:
Updated list: ['a', 'e', 'i', 'o', 'u']
mixed_list = [{1, 2}, (5, 6, 7]] # Numeric tuple number_tuple = (3, 4) # Insert tuple into list mixed_list.insert(1, number_tuple) print('Updated list: ', mixed_list)
When the program is run, the output is:
Updated list: [{1, 2}, (3, 4)),5, 6, 7]]
Please note that the index in Python starts from 0, not1.
If an element must be inserted at the4To insert an element at the3As an index. Similarly, if an element must be inserted at the2To insert an element at a bit position, it is necessary to use1As an index.