Python List Methods
Python lists are dynamic arrays that offer a rich set of built-in methods to manipulate and process data efficiently. Below is an exhaustive list of Python list methods, along with their descriptions and usage examples.
1. append()
Description: Adds a single element to the end of the list.
Syntax: list.append(element)
Example:
fruits = ['apple', 'banana']
fruits.append('cherry')
print(fruits) # Output: ['apple', 'banana', 'cherry']
2. extend()
Description: Extends the list by appending elements from an iterable (e.g., another list, tuple, or string).
Syntax: list.extend(iterable)
Example:
fruits = ['apple', 'banana']
more_fruits = ['cherry', 'date']
fruits.extend(more_fruits)
print(fruits) # Output: ['apple', 'banana', 'cherry', 'date']
3. insert()
Description: Inserts an element at a specified position.
Syntax: list.insert(index, element)
Example:
fruits = ['apple', 'cherry']
fruits.insert(1, 'banana')
print(fruits) # Output: ['apple', 'banana', 'cherry']
4. remove()
Description: Removes the first occurrence of a specified value.
Syntax: list.remove(element)
Example:
fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana')
print(fruits) # Output: ['apple', 'cherry']
5. pop()
Description: Removes and returns the element at a specified position. If no index is specified, it removes and returns the last item.
Syntax: list.pop([index])
Example:
fruits = ['apple', 'banana', 'cherry']
popped_fruit = fruits.pop(1)
print(popped_fruit) # Output: 'banana'
print(fruits) # Output: ['apple', 'cherry']
6. clear()
Description: Removes all elements from the list.
Syntax: list.clear()
Example:
fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits) # Output: []
7. index()
Description: Returns the index of the first occurrence of a specified value.
Syntax: list.index(element[, start[, end]])
Example:
fruits = ['apple', 'banana', 'cherry', 'banana']
index = fruits.index('banana')
print(index) # Output: 1
8. count()
Description: Returns the number of times a specified value appears in the list.
Syntax: list.count(element)
Example:
fruits = ['apple', 'banana', 'cherry', 'banana']
count = fruits.count('banana')
print(count) # Output: 2
9. sort()
Description: Sorts the list in ascending order by default.
Syntax: list.sort(key=None, reverse=False)
Example:
numbers = [3, 1, 4, 1, 5, 9]
numbers.sort()
print(numbers) # Output: [1, 1, 3, 4, 5, 9]
10. reverse()
Description: Reverses the order of the list in place.
Syntax: list.reverse()
Example:
fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits) # Output: ['cherry', 'banana', 'apple']
11. copy()
Description: Returns a shallow copy of the list.
Syntax: list.copy()
Example:
fruits = ['apple', 'banana', 'cherry']
fruits_copy = fruits.copy()
print(fruits_copy) # Output: ['apple', 'banana', 'cherry']
12. len()
Description: Returns the number of items in the list.
Syntax: len(list)
Example:
fruits = ['apple', 'banana', 'cherry']
print(len(fruits)) # Output: 3
13. max()
Description: Returns the largest item in the list.
Syntax: max(list)
Example:
numbers = [3, 1, 4, 1, 5, 9]
print(max(numbers)) # Output: 9
14. min()
Description: Returns the smallest item in the list.
Syntax: min(list)
Example:
numbers = [3, 1, 4, 1, 5, 9]
print(min(numbers)) # Output: 1
15. sum()
Description: Returns the sum of all items in the list.
Syntax: sum(list, start=0)
Example:
numbers = [3, 1, 4, 1, 5, 9]
print(sum(numbers)) # Output: 23
16. list()
Description: Creates a list from an iterable.
Syntax: list(iterable)
Example:
string = 'hello'
char_list = list(string)
print(char_list) # Output: ['h', 'e', 'l', 'l', 'o']
Keep Learning 🙂