Python List Methods
Python list methods are built-in functions that allow us to perform various operations on
lists, such as adding, removing, or modifying elements. Here is an overview of all Python list
methods with simple examples.
List Methods
Append: Adds an element to the end of the list.
copy(): Returns a shallow copy of the list.
clear(): Removes all elements from the list.
count(): Returns the number of times a specified element appears in the list.
extend(): Adds elements from another list to the end of the current list.
index(): Returns the index of the first occurrence of a specified element.
insert(): Inserts an element at a specified position.
pop(): Removes and returns the element at the specified position (or the last element if no
index is specified).
remove(): Removes the first occurrence of a specified element.
reverse(): Reverses the order of the elements in the list.
sort(): Sorts the list in ascending order (by default).
Examples of List Methods
append()
Syntax: list_name.append(element)
Example:
a = [1, 2, 3]
[Link](4)
print(a)
Output: [1, 2, 3, 4]
copy()
Syntax: list_name.copy()
Example:
a = [1, 2, 3]
b = [Link]()
print(b)
Output: [1, 2, 3]
clear()
Syntax: list_name.clear()
Example:
a = [1, 2, 3]
[Link]()
print(a)
Output: []
count()
Syntax: list_name.count(element)
Example:
a = [1, 2, 3, 2]
print([Link](2))
Output: 2
extend()
Syntax: list_name.extend(iterable)
Example:
a = [1, 2]
[Link]([3, 4])
print(a)
Output: [1, 2, 3, 4]
index()
Syntax: list_name.index(element)
Example:
a = [1, 2, 3]
print([Link](2))
Output: 1
insert()
Syntax: list_name.insert(index, element)
Example:
a = [1, 3]
[Link](1, 2)
print(a)
Output: [1, 2, 3]
pop()
Syntax: list_name.pop(index)
Example:
a = [1, 2, 3]
[Link]()
print(a)
Output: [1, 2]
remove()
Syntax: list_name.remove(element)
Example:
a = [1, 2, 3]
[Link](2)
print(a)
Output: [1, 3]
reverse()
Syntax: list_name.reverse()
Example:
a = [1, 2, 3]
[Link]()
print(a)
Output: [3, 2, 1]
sort()
Syntax: list_name.sort(key=None, reverse=False)
Example:
a = [3, 1, 2]
[Link]()
print(a)
Output: [1, 2, 3]