Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

What are the Methods of List in Python?

Methods of List in Python

Condition for Methods of List in Python

  • Description: List methods are built-in functions that allow you to manipulate and perform operations on lists.Lists are mutable sequences, and these methods provide a variety of ways to modify, access, and interact with list elements.
Step-by-Step Process
  • Append: It adds a single element to the end of the list.Example: list.append(item)
  • Extend: It adds all elements of an iterable(like list,tuple) to the end of the list.Example: list.extend(iterable)
  • Insert: It inserts an element at a specified index.Example: list.insert(index, item)
  • Remove: It removes the first occurrence of a specified element.Example: list.remove(item)
  • Pop: It removes and returns the element at the specified index.If no index is provided, it removes and returns the last item.Example: list.pop(index)
  • Reverse: It reverses the order of the elements in the list.Example: list.reverse()
  • Clear: It removes all elements from the list.Example: list.clear()
Sample Code
  • #Default list
    my_list = [3, 1, 4, 1, 5]
    print("Output for list methods:")
    # 1. Append
    my_list.append(9)
    print("After append:", my_list)
    # 2. Extend
    my_list.extend([2, 6, 5])
    print("After extend:", my_list)
    # 3. Remove
    my_list.remove(1)
    print("After remove:", my_list)
    # 4. Pop
    popped_item = my_list.pop(2)
    print("Popped item:", popped_item)
    print("After pop:", my_list)
    # 5. Reverse the list
    my_list.reverse()
    print("After reverse:", my_list)
    #6. insert
    my_list.insert(1,2)
    print("After insert:", my_list)
Screenshots
  • Methods of List