Python List Methods

ajith_56

AJITH D R

Posted on February 15, 2023

Python List Methods

1.append():
The append() method is used to append a value at the end of an existing list. The value to be appended is passed as an argument.

arr = []
arr.append(1)
print(arr) #[1]
Enter fullscreen mode Exit fullscreen mode

2.insert():
The insert() method is used to add an element to a list at the specified position. All the elements from the specified position will be shifted to the right. If the given position is out of range, the element will be appended at the end.

print(arr) #[1]
arr.insert(0, 10)
print(arr) #[10, 1]
Enter fullscreen mode Exit fullscreen mode

3.extend():
The extend() method takes a list of values as an argument and adds all the values to the existing list.

print(arr) #[10, 1]
arr.extend([2, 3, 4])
print(arr) #[10, 1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

4.remove():
The remove() method removes the given element from the list. If there are duplicate elements, the first occurrence will be removed, and if the given element is not in the list, exception will be raised.

print(arr) [10, 1, 2, 3, 4]
arr.remove(10)
print(arr) [1, 2, 3, 4]
arr.remove(12) #ValueError: list.remove(x): x not in list
Enter fullscreen mode Exit fullscreen mode

5.pop():
The pop() method is used to remove an element at a specified index from the list. If the index position is not given, last element will be removed.

print(arr) #[1, 2, 3, 4]
arr.pop(2)
print(arr) #[1, 2, 4]
arr.pop()
print(arr) #[1, 2]
arr.pop(4) #IndexError: pop index out of range
Enter fullscreen mode Exit fullscreen mode

6.index():
The index() method takes a value as an argument and returns the index of first occurrence position of that value. If the value is not in the list, an exception will be raised.

print(arr) #[1, 2]
print(arr.index(2)) #1
print(arr.index(3)) #ValueError: list.index(x): x not in list
Enter fullscreen mode Exit fullscreen mode

7.count():
The count() method takes a value as an argument, searches the value in the list and returns the number of times the value has repeated in the list.

print(arr) #[1, 2]
print(arr.count(2)) #1
arr.extend([2, 3, 4, 5])
print(arr.count(2)) #2
Enter fullscreen mode Exit fullscreen mode

8.reverse():
This method reverses the list and returns the reversed list as an output.

print(arr) #[1, 2, 2, 3, 4, 5]
arr.reverse()
print(arr) #[5, 4, 3, 2, 2, 1]
Enter fullscreen mode Exit fullscreen mode

9.sort():
This method sorts the list list in-place in ascending or descending order. By default, this method sorts the list in ascending order. To sort the list in the descending order we should set reverse to True in the argument.

print(arr) #[5, 4, 3, 2, 2, 1]
arr.sort()
print(arr) #[1, 2, 2, 3, 4, 5]
arr.sort(reverse=True)
print(arr) #[5, 4, 3, 2, 2, 1]
Enter fullscreen mode Exit fullscreen mode

10.copy():
This methods returns a copy of the list.

print(arr) #[5, 4, 3, 2, 2, 1]
arr2 = arr.copy()
print(arr2) #[5, 4, 3, 2, 2, 1]
Enter fullscreen mode Exit fullscreen mode

11.clear():
This method is used to remove all the values inside a list.

print(arr) #[5, 4, 3, 2, 2, 1]
arr.clear()
print(arr) #[]
Enter fullscreen mode Exit fullscreen mode

References:

💖 💪 🙅 🚩
ajith_56
AJITH D R

Posted on February 15, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Learning Python
javascript Learning Python

November 28, 2024

Calculate savings with Python!
beginners Calculate savings with Python!

November 26, 2024

UV the game-changer package manager
programming UV the game-changer package manager

November 24, 2024

Beginners Guide for Classes
python Beginners Guide for Classes

November 20, 2024