Python list functions | count(), index(), max(), min(), reverse() and sort()
count()
Definition and Usage
The count() method returns the number of value which is how
many times used in the list.
Syntax
list.count(value)
Parameter Values
Required.
Any type (string, number, list, tuple, etc.). The value to search for.
Example 1
fruits = ['apple', 'banana', 'cherry',’orange’]
x = fruits.count("cherry")
print (x)
Example 2
points = [1, 4, 2, 9, 7, 8, 9, 3, 1]
x = points.count(9)
print (x)
max()
Definition and usage
The max() function returns
the maximum value from a list.
Syntax
Max
(list)
Parameter
Values
Any
type(string, number, list, tuple, etc.)
Example
1
Li
= [10,20,30,10,40]
X
= max(Li)
Print (x)
Output: ‘40’
Example
2
Li
= [“apple”,”Orange”,”Banana”]
X
= max(Li)
Print (x)
Output: ‘Orange’
min()
Definition and usage
The min() function returns
the minimum value from a list.
Syntax
min
(list)
Parameter
Values
Any
type(string, number, list, tuple, etc.)
Example
1
Li
= [10,20,30,10,40]
X
= min(Li)
Print (x)
Output: ‘10’
Example
2
Li
= [“apple”,”Orange”,”Banana”]
X
= min(Li)
Print (x)
Output: ‘apple’
reverse()
Definition and usage
The reverse() function
returns the list value in reverse order.
Syntax
List.any
type()
Parameter
Values
Any
type()
Example
1
Li
= [10,20,30,10,40]
X
= Li.reverse()
Print (x)
Output: ’40,10,30,20,10’
Example
2
Li
= [“apple”,”Orange”,”Banana”]
X
= Li.reverse()
Print (x)
Output: ‘Banana, Orange,
apple’
sort()
Definition and usage
The sort() function
returns the list value in sort form like ascending or descending order.
Syntax
List.any
value()
List.any
value(reverse=True)
Parameter
Values
Any
type(string, number, list, tuple, etc.)
Example
1
Sort
in ascending order
Li
= [10,20,30,10,40]
X
= Li.sort()
Print (x)
Output: ’10,10,20,30,40’
Example
2
Li
= [“apple”,”Orange”,”Banana”]
X
= Li.sort()
Print (x)
Output: ‘apple, Banana,
Orange’
Example
3
Sort
in Descending order
Li
= [10,20,30,10,40]
X
= Li.sort(reverse=True)
Print (x)
Output: ’40,30,20,10,10’
Example
4
Li
= [“apple”,”Orange”,”Banana”]
X
= Li.sort(reverse=True)
Print (x)
Output: ‘Orange, Banana,
apple’
Index()
Definition and usage
The index() function
returns the position at the first occurrence of the specific value.
Syntax
List.index(element)
Parameter
Values
Any
type(string, number, list, tuple, etc.)
Example
1
Li
= [10,20,30,10,40]
X
= Li.index(30)
Print (x)
Output: ‘2’
Example
2
Li
= [“apple”,”Orange”,”Banana”]
X
= Li.index(“Orange”)
Print (x)
Output: ‘1’
Post a Comment