List Methods in Python
Python provides several built-in functions to work with lists. Some of the commonly used list methods are explained below:
a) len()
– Returns the Number of Elements in a List
The len()
function returns the total number of elements present in a list.
Example:
numbers = [10, 20, 30, 40, 50]
print(len(numbers))
Output: 5
b) min()
– Returns the Smallest Element in the List
The min()
function returns the minimum value from the list. All elements must be of the same data type.
Example:
values = [88, 75, 93, 67, 85]
print(min(values))
Output: 67
c) max()
– Returns the Largest Element in the List
The max()
function returns the highest value from the list.
Example:
marks = [45, 87, 92, 56, 78]
print(max(marks))
Output: 92
d) sum()
– Returns the Sum of All Elements in the List
The sum()
function calculates the total of all numeric elements in a list.
Example:
expenses = [100, 250, 175, 300]
print(sum(expenses))
Output: 825
Summary Table
Method | Description | Example | Output |
---|---|---|---|
len() | Returns number of elements in the list | len([1, 2, 3]) | 3 |
min() | Returns smallest element in the list | min([4, 1, 9]) | 1 |
max() | Returns largest element in the list | max([4, 1, 9]) | 9 |
sum() | Returns sum of all list elements | sum([10, 20, 30]) | 60 |