Signup/Sign In
PUBLISHED ON: FEBRUARY 16, 2021

How to add elements in python list.

In this tutorial, we will learn how to add elements to a python list. In the Python programming language, we can add the elements to the list or we can insert elements by the list methods such as append(), insert() method. The append() method adds an element at the end of the list and it takes only one parameter, if we give more than one parameter we will get an error. The insert() method adds elements at a specified position and it takes two parameters, position, and element, if we give more than one parameter we will get an error.

Example: Adding elements to the list using the append() method.

The below example shows how to add elements to the list using the append() method. First, we initialize the empty list. Next, using the append() method we are adding elements to the list.

#Intitialise empty list
list=[]
print("Elements present in lists are:",list)
#Adding elements at the end of the list using append() method.
list.append(10)
list.append(0.005)
list.append(100.25)
list.append("python")
list.append(10)
list.append("Java")
#print the list elements
print("Elements present in lists are:",list)

Once we run the program we will get the following result.

Elements present in lists are: []
Elements present in lists are: [10, 0.005, 100.25, 'python', 10, [10, 20, 30], 'Java']

Example: Adding the list inside the list using the append() method.

We can add the nested list to the defined list or empty list using the append() method.

#Intitialise list
list=["python",10.225,"Java"]
print("Elements present in lists are:",list)
#Adding nested list at the end of the list using append() method.
list.append([10,20,30])
list.append([30,40])
#print the list elements
print("Elements present in lists are:",list)

Once we run the program we will get the following result.

Elements present in lists are: ['python', 10.225, 'Java']
Elements present in lists are: ['python', 10.225, 'Java', [10, 20, 30], [30, 40]]

Example: Add n no of elements to list using the append() method.

We can add n no of elements by taking the input from the user and adding elements to the list using the append() method. In the below example, first, we initialize the empty list and take the user input, which specifies how many elements should add to the list.

We are adding elements to the list by taking input from the user using a while loop. The while loop iterates until it matches the given condition.

#Intializing empty list
list=[]
print("Elements present in lists are:",list)
user_input=int(input("Enter the number:"))
i=0
while i< user_input:
    element=input("Enter the elements:")
    list.append(element)
    i=i+1
print("Elements present in lists are:",list)

Once we run the program we will get the following result.

Elements present in lists are: []
Enter the number:5
Enter the elements:python
Enter the elements:2.012
Enter the elements:[10,20]
Enter the elements:True
Enter the elements:10
Elements present in lists are: ['python', '2.012', '[0,20]', 'True', '10']

Example: Adding Elements to the list using the insert() method.

The below example shows how to add elements to the list using the insert() method. First, we initialize the empty list. Next, using the insert() method we are adding elements to the list.

#Intializing empty list
list=[]
print("Elements present in lists are:",list)
list.insert(0,"python")
list.insert(1,"Java")
list.insert(2,10)
list.insert(3,True)
list.insert(4,0.1245478)
list.insert(5,"python")
print("Elements present in lists are:",list)

Once we run the program we will get the following result.

Elements present in lists are: []
Elements present in lists are: ['python', 'Java', 10, True, 0.1245478, 'python']

Example: Adding elements to the list using the insert() method.

Using the insert() method, we can modify the list. If we want to add an element at a particular index to the already defined list, we can use the insert() method. The below example shows the same.

#Intializing empty list
list=['python', 'Java', '10', True, 0.1245478, 'python']
print("Elements present in lists are:",list)
list.insert(1,"C")
print("Elements present in lists are:",list)
list.insert(5,10)
print("Elements present in lists are:",list)

Once we run the program we will get the following result.


Elements present in lists are: ['python', 'Java', '10', True, 0.1245478, 'python']
Elements present in lists are: ['python', 'C', 'Java', '10', True, 0.1245478, 'python']
Elements present in lists are: ['python', 'C', 'Java', '10', True, 10, 0.1245478, 'python']

Conclusion:

In this tutorial, we learned how to add elements to the list by the list append() method and insert() method. By solving examples on these two methods we added the elements to the list.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.