Open In App

List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()…)

Last Updated : 18 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Some of the list methods are mentioned in set 1 below

List Methods in Python | Set 1 (in, not in, len(), min(), max()…)

More methods are discussed in this article.

1. del[a : b] :- This method deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments.

2. pop() :- This method deletes the element at the position mentioned in its arguments.




# Python code to demonstrate the working of
# del and pop()
  
# initializing list 
lis = [2, 1, 3, 5, 4, 3, 8]
  
# using del to delete elements from pos. 2 to 5
# deletes 3,5,4
del lis[2 : 5]
  
# displaying list after deleting 
print ("List elements after deleting are : ",end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")
      
print("\r")
  
# using pop() to delete element at pos 2
# deletes 3
lis.pop(2)
  
# displaying list after popping  
print ("List elements after popping are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")


Output:

List elements after deleting are : 2 1 3 8 
List elements after popping are : 2 1 8 

3. insert(a, x) :- This function inserts an element at the position mentioned in its arguments. It takes 2 arguments, position and element to be added at respective position.

4. remove() :- This function is used to delete the first occurrence of number mentioned in its arguments.




# Python code to demonstrate the working of
# insert() and remove()
  
# initializing list 
lis = [2, 1, 3, 5, 3, 8]
  
# using insert() to insert 4 at 3rd pos
lis.insert(3, 4)
  
# displaying list after inserting
print("List elements after inserting 4 are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")
      
print("\r")
  
# using remove() to remove first occurrence of 3
# removes 3 at pos 2
lis.remove(3)
  
# displaying list after removing 
print ("List elements after removing are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")


Output:

List elements after inserting 4 are : 2 1 3 4 5 3 8 
List elements after removing are : 2 1 4 5 3 8 

5. sort() :- This function sorts the list in increasing order.

6. reverse() :- This function reverses the elements of list.




# Python code to demonstrate the working of
# sort() and reverse()
  
# initializing list 
lis = [2, 1, 3, 5, 3, 8]
  
# using sort() to sort the list
lis.sort()
  
# displaying list after sorting
print ("List elements after sorting are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")
      
print("\r")
  
# using reverse() to reverse the list
lis.reverse()
  
# displaying list after reversing
print ("List elements after reversing are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")


Output:

List elements after sorting are : 1 2 3 3 5 8 
List elements after reversing are : 8 5 3 3 2 1 

7. extend(b) :- This function is used to extend the list with the elements present in another list. This function takes another list as its argument.

8. clear() :- This function is used to erase all the elements of list. After this operation, list becomes empty.




# Python code to demonstrate the working of
# extend() and clear()
  
# initializing list 1
lis1 = [2, 1, 3, 5]
  
# initializing list 1
lis2 = [6, 4, 3]
  
# using extend() to add elements of lis2 in lis1
lis1.extend(lis2)
  
# displaying list after sorting
print ("List elements after extending are : ", end="")
for i in range(0, len(lis1)):
    print(lis1[i], end=" ")
      
print ("\r")
  
# using clear() to delete all lis1 contents
lis1.clear()
  
# displaying list after clearing
print ("List elements after clearing are : ", end="")
for i in range(0, len(lis1)):
    print(lis1[i], end=" ")


Output:

List elements after extending are : 2 1 3 5 6 4 3 
List elements after clearing are : 

Related articles:
List methods in Python
List Methods in Python | Set 1 (in, not in, len(), min(), max()…)



Similar Reads

What Is Difference Between Del, Remove and Pop on Python Lists?
In python del is a keyword and remove(), pop() are in-built methods. The purpose of these three are same but the behavior is different remove() method delete values or object from the list using value and del and pop() deletes values or object from the list using an index. del Keyword: The del keyword delete any variable, list of values from a list
3 min read
Difference between Append, Extend and Insert in Python
Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their crea
3 min read
delattr() and del() in Python
In this article, we are going to see delattr() and del() functions in Python delattr() in Python The delattr() method is used to delete the named attribute from the object, with the prior permission of the object. Syntax: delattr(object, name): The function takes only two parameter: object: from which the name attribute is to be removed.name: of th
3 min read
Python del to delete objects
The del keyword in Python is primarily used to delete objects in Python. Since everything in Python represents an object in one way or another, The del keyword can also be used to delete a list, slice a list, delete dictionaries, remove key-value pairs from a dictionary, delete variables, etc. Syntax: del object_nameBelow are various examples that
3 min read
Python List extend() Method
The Python List extend() method adds items of an iterable (list, tuple, dictionary, etc) at the end of a list. Example C/C++ Code other_cars = ["Maruti", "Tata"] cars.extend(other_cars) print(cars) Output ['Audi', 'BMW', 'Jaguar', 'Maruti', 'Tata']List extend() Syntaxlist_name.extend(iterable) Parameters:iterable: Any iterable (
4 min read
Python List Extend vs Append
Python, being a versatile and powerful programming language, provides multiple ways to manipulate lists. Two commonly used methods for adding elements to a list are extend and append. While both serve the purpose of adding elements, they differ in functionality and use cases. In this article, we'll delve into the nuances of Python's extend and appe
4 min read
Python List pop() Method
Python list pop() function removes elements at a specific index from the list. Example Python Code fruits.pop() print(fruits) Output: ['apple', 'mango']Python List pop() Syntaxlist_name.pop(index)Parameterindex (optional) - The value at the index is popped out and removed. If the index is not given, then the last element is popped out and removed.R
2 min read
IndexError: pop from Empty List in Python
The IndexError: pop from an empty list is a common issue in Python, occurring when an attempt is made to use the pop() method on a list that has no elements. This article explores the nature of this error, provides a clear example of its occurrence, and offers three practical solutions to handle it effectively. Here, we will see how to fix Pop From
3 min read
Python Set pop() Method
Python set pop() removes any random element from the set and returns the removed element. In this article, we will see about the Python set pop() method. Example Input: {9, 1, 0} Output: {9, 1} Explanation: By using set pop() method, a random element 0 is removed from the set and remaining set is returned.Python Set pop() SyntaxSyntax: set_obj.pop(
2 min read
append() and extend() in Python
Extend and Append are two Python list methods used to add elements to a list. Although they appear similar, they have different functionalities and use cases. Understanding the differences between the append() and extend() methods is crucial when working with lists in Python. Although both techniques are used to add elements to a list, their behavi
4 min read