English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Fonctions en Python

Types de données en Python

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python list insert() usage and example

Python list methods

The insert() method adds an element to the specified index in the list.

The syntax of the insert() method is

list.insert(index, element)

insert() parameters

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

insert() return value

The insert() method only inserts an element into the list. It does not return anything; it returns None.

Example1:Insert element into list

# 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']

Example2:Insert tuple (as element) into list

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.

Python list methods