How to add elements to a list in python?

Python supports various inbuilt methods to add items to the list such as append(), insert(), and extend(). Concatenation can also be used to add items to the list. The different ways of adding an item to the list are as follows.

Adding an item to the list by using the append() method

The append() method is used to add an item to the end of the list. This method takes a single argument and the argument can be numbers, lists, strings, etc. It doesn't return any value. The syntax for the append() method is as follows.

syntax:
list.append(item)

For example, let us consider a list fruit_list = ["apple", "orange","grapes"]. The item "banana" is passed as an argument to append() function. The fruit_list is now updated to ["apple", "orange","grapes", "banana"]

fruit_list = ["apple" , "orange" ,"grapes"]
print("fruit list is: ",fruit_list)
fruit_list.append("banana")
print("The updated list is: ", fruit_list)

Output

fruit list is:  ['apple', 'orange', 'grapes']
The updated list is:  ['apple', 'orange', 'grapes', 'banana']

Let us consider another example to add a tuple Tuple1 = ("papaya", "watermelon") into the list fruit_list.

fruit_list = ["apple" , "orange" ,"grapes"]
Tuple1 = ("papaya", "watermelon")
print("fruit list is: ",fruit_list)
fruit_list.append(Tuple1)
print("The updated list is: ", fruit_list)

Output

fruit list is:  ['apple', 'orange', 'grapes']
The updated list is:  ['apple', 'orange', 'grapes', ('papaya', 'watermelon')]

Adding an item to the list by using the insert() method

The insert() method is used to insert an item at a specified position in the list. This method takes two arguments. The first argument indicates the position in which the element will be inserted and the second argument represents the element. The insert() method doesn't return anything.

For example, let us consider a list fruit_list = ["apple", "orange","grapes"]. The position "1" and item "banana" is passed as an argument to insert() function. The fruit_list is now updated to ["apple", "banana","orange","grapes"].

fruit_list = ["apple" , "orange" ,"grapes"]
print("fruit list is: ",fruit_list)
fruit_list.insert(1,"banana")
print("The updated list is: ", fruit_list)

Output

fruit list is:  ['apple', 'orange', 'grapes']
The updated list is:  ['apple', 'banana', 'orange', 'grapes']

Let us consider another example to add a tuple Tuple1 = ("papaya", "watermelon") into the list fruit_list at position 1.

fruit_list = ["apple" , "orange" ,"grapes"]
Tuple1 = ("papaya", "watermelon")
print("fruit list is: ",fruit_list)
fruit_list.insert(1,Tuple1)
print("The updated list is: ", fruit_list)

Output

fruit list is:  ['apple', 'orange', 'grapes']
The updated list is:  ['apple', ('papaya', 'watermelon'), 'orange', 'grapes']

Adding an item to the list by using the extend() method

The extend() method is used to add all the items of a sequence to the end of the list. This method takes a single argument and the argument can be tuples, lists, strings, etc. It doesn't return any value. The syntax for the extend() method is as follows.

syntax:
list.extend(iterable)

For example, let us consider a list fruit_list = ["apple", "orange","grapes"]. The list2= ["papaya", "watermelon"] is passed as an argument to extend() function. The fruit_list is now updated to ["apple", "orange","grapes", "papaya", "watermelon"]

fruit_list = ["apple" , "orange" ,"grapes"]
list2 = ["papaya", "watermelon"]
print("fruit list is: ",fruit_list)
fruit_list.extend(list2)
print("The updated list is: ", fruit_list)

Output

fruit list is:  ['apple', 'orange', 'grapes']
The updated list is:  ['apple', 'orange', 'grapes', 'papaya', 'watermelon']

Adding items to the list by concatenation of lists

Concatenation is a technique of combining two or more lists. For example, let us consider list1 = [1, 2, 3] and list2 = [4, 5]. Concatenating the two lists results in [1, 2, 3, 4, 5]. The + operator can be used for concatenation.

For example, let us consider a list fruit_list1 = ["apple", "orange","grapes"] and fruit_list2 = ["papaya", "watermelon"]. The fruit_list2 is added to fruit_list1 by using + operator.

fruit_list1 = ["apple" , "orange" ,"grapes"]
fruit_list2 = ["papaya", "watermelon"]
print("fruit list1 is: ",fruit_list1)
print("fruit list2 is: ",fruit_list2)
fruit_list1 = fruit_list1 + fruit_list2
print("The updated list is: ", fruit_list1)

Output

fruit list1 is:  ['apple', 'orange', 'grapes']
fruit list2 is:  ['papaya', 'watermelon']
The updated list is:  ['apple', 'orange', 'grapes', 'papaya', 'watermelon']
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions