Adding Elements to the List
There are numerous possible ways in which we can add the elements to this list, Moreover, python has an innate function, we call it append(). Further, while using the append() function, we can only add a single element to this list. Moreover, if we want to add numerous elements to this list then we must have to use the ‘for loop’. However, the append() function adds the element at the end of the list each time we use it and this function takes only a single argument. Lets us learn to add and deleting elements in this article.
Further, if we want to add the elements at a particular position then we just have to use the insert() method. insert() takes 2 arguments i.e. position and the value. Moreover, the position refers to the index and here, the elements need to be added. In addition, here the value refers to the element that should be added to the list.
Thus, there is one more method that we call extend(). Thereby, using this method we can add the elements to the list. Moreover, we use the extend() method to add a list of the elements to the list.
Example No.1:
List = [“Hey”, “Good Evening”]
print(“List before appending values is: “, List)
List.append(“Python”)
List.append(“Hey”)
print(“List after appending values is: ”, List)
Output:
List before appending values is: [“Hey”, “Good Evening”]
List after appending values is: [“Hey”, “Good Evening”, “Python”, “Hey”]
In the above example, we are appending ‘Python’ and ‘Hey’ values to the end of the List.
Example No.2:
List = [“Apple”, “Orange”, “Mango”, “Banana”]
print(“List before inserting is: “, List)
List.insert(2, “Papaya”)
print(“List after inserting is: “, List)
Output:
List before inserting is: [“Apple”, “Orange”, “Mango”, “Banana”]
List after inserting is: [“Apple”, “Orange”, “Papaya”, “Mango”, “Banana”]
Browse more Topics under Lists
- Creating Lists
- Initializing and Accessing the Elements
- Traversing Lists
- Appending Lists
- Composition
- Lists as Arguments
- List Functions and Methods
Deleting Elements from a List
We can delete or remove the elements from the list using del and remove() statements too.
Thus, let’s have a look at the examples present below:
Example No.1:
List = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(“List before deleting 3rd element is: ”, List)
del List[3]
print(“List after deleting 3rd element is: ”, List)
del List[1:3]
print(“List after deleting multiple elements is: “, List)
Output:
List before deleting 3rd element is: [1, 2, 3, 4, 5, 6, 7, 8, 9]
List after deleting 3rd element is: [1, 2, 3, 5, 6, 7, 8, 9]
List after deleting multiple elements is: [1, 5, 6, 7, 8, 9]
In the example available above, we can notice that we have used the ‘del’ statement for deleting an element or multiple statements from this list.
Example No.2:
List = [1, 2, 3, 4, 5, 6, 7]
print(“List before removing a element is: “, List)
List.remove(3)
print(“List after removing a element is: “, List)
List.pop()
print(“List after poping the element is: “, List)
Output:
List before removing an element is: [1, 2, 3, 4, 5, 6, 7]
List after removing an element is: [1, 2, 4, 5, 6, 7]
List after popping the element is: [1, 2, 4, 5, 6]
In the example present above, we can notice that we are removing an element from this list with the use of the remove() method. Moreover, we use the pop() method to remove or delete the last element in the list.
FAQs on Updating and Deleting Elements
Question 1: Describe how can we delete elements from a list in Python?
Answer: In Python, we can use list methods clear(), pop(), and remove() to remove the elements from the list.
Question 2: How do we clear all the elements from a list in Python?
Answer: These are the following ways:
Method No.1: Using the clear() method.
Method No.2: Reinitializing the list.
Method No.3: Using the “*= 0” function.
Leave a Reply