0% found this document useful (0 votes)
264 views1 page

Python List Methods Cheat Sheet

The document provides a cheat sheet of common list methods in Python. It includes the method name, description, and example use for each of the following list methods: append(), clear(), copy(), count(), extend(), index(), insert(), pop(), remove(), reverse(), and sort(). The cheat sheet is intended to help users learn, code, and play with Python list methods through examples and explanations.

Uploaded by

Meenakshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
264 views1 page

Python List Methods Cheat Sheet

The document provides a cheat sheet of common list methods in Python. It includes the method name, description, and example use for each of the following list methods: append(), clear(), copy(), count(), extend(), index(), insert(), pop(), remove(), reverse(), and sort(). The cheat sheet is intended to help users learn, code, and play with Python list methods through examples and explanations.

Uploaded by

Meenakshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Python List Methods Overview

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]

Python Cheat Sheet: List Methods 
“​A puzzle a day to learn, code, and play​” → Visit ​finxter.com 
 
Method 
Description 
Ex

You might also like