List Methods Guide
Here are a few useful list methods that work on list objects in Python. For a full list of list methods, refer to the Python reference here: Python Reference List.
append(element)
:- Description: Adds an element to the end of the list.
- Parameters:
element
: The element to add to the list.
- Example:
fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits) # Outputs: ['apple', 'banana', 'cherry', 'orange']
extend(iterable)
:- Description: Extends the list by appending all the elements from another iterable (e.g., another list).
- Parameters:
iterable
: Any iterable (like a list or tuple) whose elements will be added to the list.
- Example:
fruits = ["apple", "banana", "cherry"] more_fruits = ["mango", "pineapple"] fruits.extend(more_fruits) print(fruits) # Outputs: ['apple', 'banana', 'cherry', 'mango', 'pineapple']
insert(index, element)
:- Description: Inserts an element at a specified position.
- Parameters:
index
: The position where the element should be inserted.element
: The element to insert into the list.
- Example:
fruits = ["apple", "banana", "cherry"] fruits.insert(1, "orange") print(fruits) # Outputs: ['apple', 'orange', 'banana', 'cherry']
remove(element)
:- Description: Removes the first occurrence of a specified element from the list.
- Parameters:
element
: The element to be removed.
- Example:
fruits = ["apple", "banana", "cherry", "banana"] fruits.remove("banana") print(fruits) # Outputs: ['apple', 'cherry', 'banana']
pop(index)
:- Description: Removes and returns the element at a specified position. If no index is specified, removes the last element.
- Parameters:
index
(optional): The index of the element to remove. Defaults to the last item if not specified.
- Example:
fruits = ["apple", "banana", "cherry"] fruit = fruits.pop(1) print(fruit) # Outputs: 'banana' print(fruits) # Outputs: ['apple', 'cherry']
index(element)
:- Description: Returns the index of the first occurrence of a specified element in the list. If the element is not found, it raises a
ValueError
. - Parameters:
element
: The element whose index needs to be found.start
(optional): The position to start the search. Defaults to the beginning of the list.end
(optional): The position to end the search. Defaults to the end of the list.
- Example:
fruits = ["apple", "banana", "cherry"] position = fruits.index("banana") print(position) # Outputs: 1
- Description: Returns the index of the first occurrence of a specified element in the list. If the element is not found, it raises a
count(element)
:- Description: Returns the number of times the specified element appears in the list.
- Parameters:
element
: The element whose occurrences need to be counted.
- Example:
fruits = ["apple", "banana", "cherry", "banana"] count_bananas = fruits.count("banana") print(count_bananas) # Outputs: 2
sort()
:- Description: Sorts the list in ascending order. If
reverse=True
is specified, sorts in descending order. - Parameters:
key
(optional): A function that serves as a key for the sort comparison.reverse
(optional): IfTrue
, sorts the list in descending order. Defaults toFalse
.
- Example:
fruits = ["apple", "banana", "cherry"] fruits.sort() print(fruits) # Outputs: ['apple', 'banana', 'cherry'] fruits.sort(reverse=True) print(fruits) # Outputs: ['cherry', 'banana', 'apple']
- Description: Sorts the list in ascending order. If
reverse()
:- Description: Reverses the elements of the list in place.
- Parameters: This method does not take any parameters.
- Example:
fruits = ["apple", "banana", "cherry"] fruits.reverse() print(fruits) # Outputs: ['cherry', 'banana', 'apple']
clear()
:- Description: Removes all elements from the list, leaving it empty.
- Parameters: This method does not take any parameters.
- Example:
fruits = ["apple", "banana", "cherry"] fruits.clear() print(fruits) # Outputs: []
copy()
:- Description: Returns a shallow copy of the list.
- Parameters: This method does not take any parameters.
- Example:
fruits = ["apple", "banana", "cherry"] fruits_copy = fruits.copy() print(fruits_copy) # Outputs: ['apple', 'banana', 'cherry']