List Methods in Python

Video Tutorial
FREE
List Operations thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

There are several built-in list Methods in python. It is used to used to do operation in python's list. There are methods like append(), pop(), insert() and many more. These methods helps developers to efficiently to modify data structures to meet their programming needs. In this article, we will discuss list methods in python with examples.

Python List Methods

Here are some list methods in Python:

MethodDescription
append()It adds an element to the end of the list.
extend()Appends the contents of another list to the end.
insert()It inserts an element at the given position in the list.
remove()Removes the first occurrence of a given value.
pop()It removes the element at the given position.
clear()It is used to remove all the elements in the given list.
index()It returns the index of the first occurrence of a value.
count()Returns the number of occurrences of a value.
sort()sort() function sorts the list either in ascending or descending order(with some optional parameters) .
reverse()Reverses the order of the list.

Built-in Python Methods to add Element in a list

There are several built-in list methods in python used to add an element to a list. Below are the examples given with the syntax along with their description.

  1. append():

    • Syntax: list.append(element)
    • Description: Appends an element to the end of the list.
    • Example:
    • Output
  2. extend():

    • Syntax: list.extend(iterable)
    • Description: Appends the elements of an iterable (such as another list) to the end of the list.
    • Example:
    • Output
  3. insert():

    • Syntax: list.insert(index, element)
    • Description: Inserts an element at the specified position in the list.
    • Example:
    • Output

Essential Python list Methods

Here are some important list methods in python along with their simple description, syntax and example:

  1. Python sum() method

    Syntax of sum() method:

    Description:

    It returns the sum of all elements in the iterable. Optionally, you can specify a start value.

    Example:

    Output

  2. Python count()

    Syntax of python count():

    Description:

    It returns the number of occurrences of a given element in the list.

    Example:

    Output

  3. Python len()

    Syntax of python count()

    Description:

    python len() returns the number of elements in the list.

    Example:

    Output

  4. Python min()

    Syntax of Python min():

    Description:

    min() in python returns the smallest element in the list.

    Example:

  5. Python max()

    Syntax of Python max():

    Description:

    Returns the largest element in the list.

    Example:

    Output

  6. Python sort()

    Syntax of Python sort():

    list.sort(reverse=True)

    Description:

    • It is used to sort the elements given in the list in ascending order. There are some optional parameters given which allow us to sort in reverse order.
    • This reverse= true is used to make the list in descending order.

    Example:

    Output

  7. Python reverse()

    Syntax of Python reverse():

    Description: reverse() method in Python reverses the order of elements given in the list.

    Example

    Output

Python Methods used to Delete one or More Elements

Deleting elements from a list is a common operation in Python programming. Here are some list methods in python like remove() , pop(), del and clear() are used to delete elements. Below are the methods or functions along with their descriptions, syntax, and examples:

  1. remove() Method:

    Syntax of remove() method:

    Description:

    remove() in python removes the first occurrence of the given element from the list.

    Example:

    Output

  2. pop() Method:

    Syntax of pop() method:

    Description:

    The pop() method in Python removes and returns the element at the given index. If no index is given, it removes and returns the last element.

    Example:

    Output

  3. del Statement:

    Syntax of del statement:

    del list[index] or del list[start:end]

    Description: del removes the element at the given index.

    Example:

    Output

  4. clear() Method:

    Syntax of clear() method:

    list.clear()

    Description:

    clear() method in python removes all elements from the list, leaving it empty.

    Example:

    Output

Conclusion

  1. Several list methods in Python are used to perform different operations on the list.
  2. We learned about append, pop() method, clear() in python etc to perform operations.
  3. There are various methods of list in Python used to add, remove, sort, finding the maximum and minimum in the list.
  4. These methods enhance the functionality and flexibility of Python lists.

Read More