Pyt 4
Pyt 4
Python also has list methods already implemented to help us perform common list operations.
Here are some examples of the most commonly used list methods:
>>> my_list.append(5)
>>> my_list
[1, 2, 3, 3, 4, 5]
>>> my_list.remove(2)
>>> my_list
[1, 15, 3, 3, 4, 5, 6, 7, 8, 2, 2]
>>> my_list.pop()
2
>>> my_list.index(6)
6
>>> my_list.count(2)
1
>>> my_list.sort()
>>> my_list
[1, 2, 3, 3, 4, 5, 6, 7, 8, 15]
>>> my_list.reverse()
>>> my_list
[15, 8, 7, 6, 5, 4, 3, 3, 2, 1]
>>> my_list.clear()
>>> my_list
[]
To learn more about list methods, I would recommend reading this article from the Python
documentation.