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

Python List Functions CheatSheet

Uploaded by

ehtsalman
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)
4 views1 page

Python List Functions CheatSheet

Uploaded by

ehtsalman
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 Functions & Methods Cheat Sheet

Built-in Functions for Lists


These functions work directly with list objects:

- len(list): Returns number of elements.


- max(list): Largest element.
- min(list): Smallest element.
- sum(list): Sum of numeric elements.
- sorted(list): Returns a new sorted list.
- list(iterable): Creates a list from an iterable.

Important List Methods


Called on list objects:

- append(x): Adds element x to end of list.


- extend(iterable): Adds all elements from another iterable.
- insert(i, x): Inserts x at position i.
- remove(x): Removes first occurrence of x.
- pop([i]): Removes and returns element at index i (default last).
- clear(): Removes all elements.
- index(x): Returns index of first occurrence of x.
- count(x): Returns number of occurrences of x.
- sort(reverse=False): Sorts list in-place.
- reverse(): Reverses list in-place.
- copy(): Returns a shallow copy of the list.

Example Usage
numbers = [10, 5, 8, 5]
[Link](20) # [10, 5, 8, 5, 20]
[Link](2, 15) # [10, 5, 15, 8, 5, 20]
[Link](5) # [10, 15, 8, 5, 20]
last = [Link]() # Removes 20 → [10, 15, 8, 5]
[Link]() # [5, 8, 10, 15]
[Link]() # [15, 10, 8, 5]
print([Link](5)) # 1

You might also like