List Methods in Python

In this post, we will learn about the list Methods. Remove Pop clear Remove Method: The remove() method searches for the given element in the list and remove first matching element. Syntax :- list.remove(element) Remove() Parameters:- The remove() method takes a single element as an argument and remove it from the list. If the element doesn't exits, it throws ValueError:list.remove(x): X not in list exception. Return value from remove(): The remove() doesn't return any value. Example:- list1=[10,20,30,40]
print(list1) Output: [10, 20, 30, 40]
[10, 20, 40] Pop Method: The pop() method removes and returns the element at the given index from the list. The syntax of the pop() method is:- list.pop(index) pop() parameters: The pop() method takes a single argument(index). The argument passed to the method is optional. If not passed, the default index -1 is passed as an argument(index of the last item). If the index passed to the method is not in rang, it throws IndexError:pop index out of range exception. Return Value from pop(): The pop() method returns the item present at the given index. This item is also removed from the list. Example: list1 = [10,20,30,40]
print(list1) Output: [10, 20, 30, 40]
[10, 20, 30] Clear Method: The clear() method removes all items from the list. The syntax of clear() method is: list.clear() Clear() parameters: The clear() method doesn't take any parameters. Return Value from clear(): The clear() method only empties the given list. it doesn't return any value. Example: list1 = [1,2,3,4]
print(list1) Output: [1, 2, 3, 4]
[] Happy Coding! Follow us on Instagram @programmersdoor Join us on Telegram @programmersdoor Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem. Follow Programmers Door for more. #blog #interview #placement #learn #computer #science

        Contact Us

  • LinkedIn
  • Facebook
  • Instagram

©2023 by Programmers Door