Python provides a wide range of built-in functions and methods to work with lists. Here’s a comprehensive list of them, grouped by functionality:
-
append()
: Adds a single element to the end of the list.fruits = ["apple", "banana"] fruits.append("cherry") print(fruits) # Output: ["apple", "banana", "cherry"]
-
extend()
: Adds elements from another iterable (e.g., list, tuple) to the end of the list.fruits = ["apple", "banana"] fruits.extend(["cherry", "date"]) print(fruits) # Output: ["apple", "banana", "cherry", "date"]
-
insert()
: Inserts an element at a specified index.fruits = ["apple", "banana"] fruits.insert(1, "cherry") print(fruits) # Output: ["apple", "cherry", "banana"]
-
remove()
: Removes the first occurrence of a specified value.fruits = ["apple", "banana", "cherry"] fruits.remove("banana") print(fruits) # Output: ["apple", "cherry"]
-
pop()
: Removes and returns an element at a specified index (default is the last element).fruits = ["apple", "banana", "cherry"] removed = fruits.pop(1) print(removed) # Output: "banana" print(fruits) # Output: ["apple", "cherry"]
-
clear()
: Removes all elements from the list.fruits = ["apple", "banana"] fruits.clear() print(fruits) # Output: []
-
index()
: Returns the index of the first occurrence of a specified value.fruits = ["apple", "banana", "cherry"] idx = fruits.index("banana") print(idx) # Output: 1
-
count()
: Returns the number of occurrences of a specified value.fruits = ["apple", "banana", "apple"] cnt = fruits.count("apple") print(cnt) # Output: 2
-
sort()
: Sorts the list in ascending order (or descending withreverse=True
).numbers = [3, 1, 4, 2] numbers.sort() print(numbers) # Output: [1, 2, 3, 4]
To sort in descending order:
numbers.sort(reverse=True) print(numbers) # Output: [4, 3, 2, 1]
-
reverse()
: Reverses the elements of the list in place.fruits = ["apple", "banana", "cherry"] fruits.reverse() print(fruits) # Output: ["cherry", "banana", "apple"]
copy()
: Returns a shallow copy of the list.fruits = ["apple", "banana"] new_fruits = fruits.copy() print(new_fruits) # Output: ["apple", "banana"]
- List comprehension: A concise way to create lists based on existing iterables.
squares = [x**2 for x in range(5)] print(squares) # Output: [0, 1, 4, 9, 16]
-
len()
: Returns the number of elements in the list.fruits = ["apple", "banana"] print(len(fruits)) # Output: 2
-
max()
: Returns the maximum value in the list.numbers = [3, 1, 4, 2] print(max(numbers)) # Output: 4
-
min()
: Returns the minimum value in the list.numbers = [3, 1, 4, 2] print(min(numbers)) # Output: 1
-
sum()
: Returns the sum of all numeric elements in the list.numbers = [3, 1, 4, 2] print(sum(numbers)) # Output: 10
-
any()
: ReturnsTrue
if any element in the list is truthy.values = [0, False, 3] print(any(values)) # Output: True
-
all()
: ReturnsTrue
if all elements in the list are truthy.values = [1, True, 3] print(all(values)) # Output: True
join()
: Joins elements of a list into a string (only for strings).fruits = ["apple", "banana", "cherry"] result = ", ".join(fruits) print(result) # Output: "apple, banana, cherry"
You can use these functions and methods to effectively manipulate and query lists in Python.****