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

18 Sep, 2018
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,4del 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 3lis.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 poslis.insert(3, 4)  # displaying list after insertingprint("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 2lis.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 listlis.sort()  # displaying list after sortingprint ("List elements after sorting are : ", end="")for i in range(0, len(lis)):    print(lis[i], end=" ")      print("\r")  # using reverse() to reverse the listlis.reverse()  # displaying list after reversingprint ("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 1lis1 = [2, 1, 3, 5]  # initializing list 1lis2 = [6, 4, 3]  # using extend() to add elements of lis2 in lis1lis1.extend(lis2)  # displaying list after sortingprint ("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 contentslis1.clear()  # displaying list after clearingprint ("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 PythonList Methods in Python | Set 1 (in, not in, len(), min(), max()...)
This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
python-list
python-list-functions
Python
School Programming
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
*args and **kwargs in Python
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects
Interfaces in Java