Python Cheat Sheet: List Methods
“A puzzle a day to learn, code, and play” → Visit [Link]
Method Description Example
[Link](x) Appends element x to the list lst. >>> l = []
>>> [Link](42)
>>> [Link](21)
[42, 21]
[Link]() Removes all elements from the list >>> lst = [1, 2, 3, 4, 5]
lst–which becomes empty. >>> [Link]()
[]
[Link]() Returns a copy of the list lst. Copies only >>> lst = [1, 2, 3]
the list, not the elements in the list (shallow >>> [Link]()
[1, 2, 3]
copy).
[Link](x) Counts the number of occurrences of >>> lst = [1, 2, 42, 2, 1, 42, 42]
element x in the list lst. >>> [Link](42)
3
>>> [Link](2)
2
[Link](iter) Adds all elements of an iterable iter (e.g. >>> lst = [1, 2, 3]
another list) to the list lst. >>> [Link]([4, 5, 6])
[1, 2, 3, 4, 5, 6]
[Link](x) Returns the position (index) of the first >>> lst = ["Alice", 42, "Bob", 99]
occurrence of value x in the list lst. >>> [Link]("Alice")
0
>>> [Link](99, 1, 3)
ValueError: 99 is not in list
[Link](i, x) Inserts element x at position (index) i
in >>> lst = [1, 2, 3, 4]
the list lst. >>> [Link](3, 99)
[1, 2, 3, 99, 4]
[Link]() Removes and returns the final element of >>> lst = [1, 2, 3]
the list lst. >>> [Link]()
3
>>> lst
[1, 2]
[Link](x) Removes and returns the first occurrence >>> lst = [1, 2, 99, 4, 99]
of element x in the list lst. >>> [Link](99)
>>> lst
[1, 2, 4, 99]
[Link]() Reverses the order of elements in the list >>> lst = [1, 2, 3, 4]
lst. >>> [Link]()
>>> lst
[4, 3, 2, 1]
[Link]() Sorts the elements in the list lst in >>> lst = [88, 12, 42, 11, 2]
ascending order. >>> [Link]()
# [2, 11, 12, 42, 88]
>>> [Link](key=lambda x: str(x)[0])
# [11, 12, 2, 42, 88]