In this tutorial, we will learn about the Python List insert[] method with the help of examples.
The insert[]
method inserts an element to the list at the specified index.
Example
# create a list of vowels
vowel = ['a', 'e', 'i', 'u']
# 'o' is inserted at index 3 [4th position]
vowel.insert[3, 'o']
print['List:', vowel]
# Output: List: ['a', 'e', 'i', 'o', 'u']
Syntax of List insert[]
The syntax of the insert[]
method is
list.insert[i, elem]
Here, elem is inserted to the list at the ith index.
All the elements after elem
are shifted to the right.
insert[] Parameters
The insert[]
method takes two parameters:
- index - the index where the element needs to be inserted
- element - this is the element to be inserted in the list
Notes:
- If index is 0, the element is inserted at the beginning of the list.
- If index is 3, the index of the inserted element will be 3 [4th element in the list].
Return Value from insert[]
The insert[]
method doesn't return anything; returns None
. It only updates the current list.
Example 1: Inserting an Element to the List
# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]
# insert 11 at index 4
prime_numbers.insert[4, 11]
print['List:', prime_numbers]
Output
List: [2, 3, 5, 7, 11]
Example 2: Inserting a Tuple [as an Element] to the List
mixed_list = [{1, 2}, [5, 6, 7]]
# number tuple
number_tuple = [3, 4]
# inserting a tuple to the list
mixed_list.insert[1, number_tuple]
print['Updated List:', mixed_list]
Output
Updated List: [{1, 2}, [3, 4], [5, 6, 7]]
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Sometimes, while dealing with strings, we may encounter a problem in which we might have a numeric variable whose value keeps changing and we need to print the string including that number. Strings and numbers being different data types have to be solved in different ways. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using Type conversion The simplest way in which this task can be performed is by converting the integer explicitly into string datatype using the basic type conversion and adding it to appropriate position.
Python3
test_str
=
"Geeks"
test_int
=
4
print
["The original string
is
: "
+
test_str]
print
["The original number : "
+
str
[test_int]]
res
=
test_str
+
str
[test_int]
+
test_str
print
["The string after adding number
is
: "
+
str
[res]]
Output :
The original string is : Geeks The original number : 4 The string after adding number is : Geeks4Geeks
Method #2: Using %d operator This operator can be used to format the string to add the integer. The “d” represents that the datatype to be inserted to string is an integer. This can be changed according to the requirements.
Python3
test_str
=
"Geeks"
test_int
=
4
print
["The original string
is
: "
+
test_str]
print
["The original number : "
+
str
[test_int]]
res
=
[test_str
+
"
%
d"
+
test_str]
%
test_int
print
["The string after adding number
is
: "
+
str
[res]]
Output :
The original string is : Geeks The original number : 4 The string after adding number is : Geeks4Geeks
Method #3: Using join[] method
Python3
test_str
=
"Geeks"
test_int
=
4
print
[
"The original string is : "
+
test_str]
print
[
"The original number : "
+
str
[test_int]]
res
=
[test_str]
*
2
res
=
str
[test_int].join[res]
print
[
"The string after adding number is : "
+
str
[res]]
Output
The original string is : Geeks The original number : 4 The string after adding number is : Geeks4Geeks