Page Contents
In this tutorial we will learn how to add an element to a list in Python . Python list has 3 built-in methods to add an element to the list . They are as follows –
- Python List
append()
method - Python List
extend()
method - Python List
insert()
method
We can also use arithmetic addition(+
) operator to add element to a list .
Now we will see about all these methods with examples –
Python List append() method
Python list append()
method helps us to add an item to the end of a list .
Syntax of append() method
list.append(Object)
Parameters of append() method
Object = Object can be of any type .
Return type of append() method
It returns None
.
Examples of Python List append() method
Example 1: Add a string to the end of a list in Python
# Declaring a list
mylist = ["red","yellow","blue","green"]
# adding an element at the end
mylist.append("purple")
# printing the updated list
print(mylist)
['red', 'yellow', 'blue', 'green', 'purple']
- In the above code first we have declared a list named
mylist
as["red","yellow","blue","green"]
. - After that we have added
"purple"
in the end of the list usingappend()
method . - Then we have printed the list . From the output we can see
"purple"
has been added in the end of the list .
Example 2: Add a list to the end of a list in Python
# Declaring two list
mylist = ["red","yellow","blue","green"]
mylist1=["black","white"]
# add a list to the end of a list
mylist.append(mylist1)
# printing the updated list
print(mylist)
['red', 'yellow', 'blue', 'green', ['black', 'white']]
In the above code we have added a list to the end of a list .
Python List insert() method
Python list insert()
method helps us to add an item in any position of a list .
Syntax of insert() method
list.insert(Index,Object)
Parameters of insert() method
- Index = It is a number specifying the position of insertion .
- Object = Object can be of any type .
Return type of insert() method
It returns None
.
Examples of Python List insert() method
Example 1: Add an element to any index in the list in Python
# Declaring a list
mylist = ["red","yellow","blue","green"]
# adding an element at 2nd position
mylist.insert(1,"grey")
# printing the updated list
print(mylist)
['red', 'grey', 'yellow', 'blue', 'green']
- In the above code first we have declared a list named
mylist
as["red","yellow","blue","green"]
. - Then we have added
"grey"
in the second position in the list . Index of second position is 1. - After that we have printed the list . From the output we can see that
"grey"
is added in the second position of the list .
Example 2: Add an element at the start of a list in Python
# Declaring a list
mylist = ["red","yellow","blue","green"]
# adding an element at 1st position
mylist.insert(0,"grey")
# printing the updated list
print(mylist)
['grey', 'red', 'yellow', 'blue', 'green']
In the above code we have added an element at the start of a list .
Python List extend() method
Python list extend()
method helps us to add all elements from an iterable to the end of a list .
Syntax of extend() method
list.extend(Iterable)
Parameters of extend() method
Iterable = Iterable means any of the Python iterable .
Return type of extend() method
It returns None
.
Examples of Python List extend() method
Example 1: Join two list in Python
# Declaring two list
mylist = ["red","yellow","blue","green"]
mylist1=["black","white"]
# adding mylist1 after mylist
mylist.extend(mylist1)
# printing the updated list
print(mylist)
['red', 'yellow', 'blue', 'green', 'black', 'white']
- In the above code first we have declared two lists
mylist
andmylist1
as["red", "yellow", "blue", "green"]
and["black","white"]
. - Then we have joined two lists using
extend()
method . - After that we have printed the list . From the output we have seen that the two lists are joined .
Example 2: Join list with tuple in Python
# Declaring a list
mylist = ["red","yellow","blue","green"]
# declaring a tuple
mytuple=("black","white")
# adding mytuple after mylist
mylist.extend(mytuple)
# printing the updated list
print(mylist)
['red', 'yellow', 'blue', 'green', 'black', 'white']
In the above code we have joined a list with tuple using extend()
method .
Example 3: Join list with dictionary in Python
# Declaring a list
mylist = ["red","yellow","blue","green"]
# declaring a dictionary
mydictionary={"black":1,"white":2}
# adding mydictionary after mylist
mylist.extend(mydictionary)
# printing the updated list
print(mylist)
['red', 'yellow', 'blue', 'green', 'black', 'white']
In the above code we have joined a list with dictionary using extend()
method .
Example 4: Join list with set in Python
# Declaring a list
mylist = ["red","yellow","blue","green"]
# declaring a set
myset={"black","white"}
# adding myset after mylist
mylist.extend(myset)
# printing the updated list
print(mylist)
['red', 'yellow', 'blue', 'green', 'white', 'black']
In the above code we have joined a list with set using extend()
method .
In this way we can join list with every iterable in Python .
Concatenate multiple lists using + operator
Earlier we have seen how to concatenate two lists in Python using Python list extend()
method . But we can not concatenate multiple lists using extend()
method . To concatenate multiple lists we will use arithmetic addition (+
) operator .
Example :
# Declaring three list
mylist = ["red","yellow","blue","green"]
mylist1=["black","white"]
mylist2=["grey","black"]
# concatenate three list
mylist=mylist+mylist1+mylist2
# printing the updated list
print(mylist)
['red', 'yellow', 'blue', 'green', 'black', 'white', 'grey', 'black']
In the above code we have concatenated three list using arithmetic addition(+
) operator .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .