TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python List insert() method with Examples

Python List insert() method with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.

<< Back to PYTHON

Python List insert(i,x) Method

Python insert() method inserts the element at the specified index in the list. The first argument is the index of the element before which to insert the element.

Signature

insert(i, x)

Parameters

i : index at which element would be inserted.

x : element to be inserted.

Return

It does not return any value rather modifies the list.

Let's see some examples of insert() method to understand it's functionality.

Python List insert() Method Example 1

Let's see an example to insert an element at 3 index of the list.

# Python list insert() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
list.insert(3,4)
print("After extending:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After extending:
1
2
3
4

Python List insert() Method Example : list

It is possible to insert a list as an element to the list. See the example where a list is inserted at specified index.

# Python list insert() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
    
list.insert(3,['4','5','6'])
print("After inserting:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After inserting:
1
2
3
['4', '5', '6']

Python List insert() Method Example : Tuple

It is possible to insert a tuple as an element to the list. See the example where a tuple is inserted at specified index.

# Python list insert() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
    
list.insert(3,('4','5','6'))
print("After inserting:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After inserting:
1
2
3
('4', '5', '6')

Python List insert() Method Example : Set

It is possible to insert a set as an element to the list. See the example where a set is inserted at specified index.

# Python list insert() Method
# Creating a list
list = ['1','2','3']
for l in list:  # Iterating list
    print(l)
    
list.insert(3,{'4','5','6'})
print("After inserting:")
for l in list:  # Iterating list
    print(l)

Output:

1
2
3
After inserting:
1
2
3
{'4', '5', '6'}

Next TopicPython Lists




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf