List Methods

In Python, list methods are built-in functions that operate on list objects. These methods provide a variety of operations for modifying, searching, and manipulating lists. Here are some common list methods:

  1. list.append(x):

    • Adds an element x to the end of the list.
    my_list = [1, 2, 3]
    my_list.append(4)  # Result: [1, 2, 3, 4]
    
  2. list.extend(iterable):

    • Extends the list by appending elements from the iterable (e.g., another list).
    my_list = [1, 2, 3]
    my_list.extend([4, 5, 6])  # Result: [1, 2, 3, 4, 5, 6]
    
  3. list.insert(index, x):

    • Inserts an element x at the specified index in the list.
    my_list = [1, 2, 3]
    my_list.insert(1, 4)  # Result: [1, 4, 2, 3]
    
  4. list.remove(x):

    • Removes the first occurrence of element x from the list.
    my_list = [1, 2, 3, 2]
    my_list.remove(2)  # Result: [1, 3, 2]
    
  5. list.pop([index]):

    • Removes and returns the element at the specified index. If no index is provided, it removes and returns the last element.
    my_list = [1, 2, 3]
    popped_element = my_list.pop(1)  # Result: [1, 3], popped_element: 2
    
  6. list.index(x[, start[, end]]):

    • Returns the index of the first occurrence of element x in the list. Raises a ValueError if x is not found.
    my_list = [1, 2, 3, 2]
    index = my_list.index(2)  # Result: 1
    
  7. list.count(x):

    • Returns the number of occurrences of element x in the list.
    my_list = [1, 2, 3, 2]
    count = my_list.count(2)  # Result: 2
    
  8. list.sort(key=None, reverse=False):

    • Sorts the elements of the list in ascending order. The key and reverse parameters allow customization of the sorting behavior.
    my_list = [3, 1, 4, 1, 5, 9, 2]
    my_list.sort()  # Result: [1, 1, 2, 3, 4, 5, 9]
    
  9. list.reverse():

    • Reverses the elements of the list in place.
    my_list = [1, 2, 3]
    my_list.reverse()  # Result: [3, 2, 1]
    
  10. list.copy():

  • Returns a shallow copy of the list.
my_list = [1, 2, 3]
copied_list = my_list.copy()  # Result: [1, 2, 3]
  1. list.clear():
  • Removes all elements from the list, leaving it empty.
my_list = [1, 2, 3]
my_list.clear()  # Result: []

These are just a few examples of the many list methods available in Python. List methods provide powerful tools for working with lists and are instrumental in various programming tasks.

© NareshShahi.com, All Right Reserved.
The theme
provider
& distributor