List Methods Reference
Python’s lists come with a powerful set of built-in methods. These methods can be categorized into two groups: those that mutate (modify) the list in-place, and those that are non-mutating, returning a new value without changing the original list.
1. Mutating Methods (Modify the List In-Place)
These methods change the list they are called on. They typically return None
to signal that the change happened in-place.
list.append(item)
Adds a single item
to the very end of the list. This is one of the most commonly used list methods.
Pyground
You have a list of tasks. Add a new task, 'File TPS reports', to the end.
Expected Output:
['Review code', 'Write documentation', 'File TPS reports']
Output:
For sorting, the built-in function sorted(iterable)
provides the same functionality but returns a new, sorted list instead of modifying the original in-place.
2. Non-Mutating Methods (Return a Value)
These methods inspect the list and return a value without changing the original list.
list.count(value)
Returns the number of times a value
appears in the list.
Pyground
Count how many 'A' grades are in a list of student scores.
Expected Output:
There are 3 'A' grades.