Built-in methods for Lists in Python
In this article, we will be learning the built-in functions of lists in Python. We will be learning different ways to use built-in methods that Python supports for lists. We will only be seeing some of the built-in methods. Therefore, to learn more about lists and their associated methods please refer to the articles called Lists in Python | Part 1 and Lists in Python | Part 2.
Before jumping into the methods, let us define built-in methods.
What are built-in methods?
The Python interpreter has several functions and types built into it that are always available. Also, there are built-in methods we can use concerning lists in python.
The below code snippet shows the list methods:
# printing all the methods of a list list = [] a = dir(list) print(a)
Output:
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
The dir() method returns a list of all the list methods in the form of a list.
Now let us look at a few frequently used methods with lists:
1. index(): The index method returns the index of a given element in a list. The method takes the value of the element as a parameter.
Syntax:
variable_name = list_name.index(value)
The below code snippet shows the working of the index() method:
# creating a list list1 = [1, 2, 3, 4, 5, 6] # obtaining the index of an element a = list1.index(2) print('The index of the element is:', a)
Output:
The index of the element is: 1
2. clear(): The clear() method removes all the elements from the list. The method does not delete the list itself; it deletes just the elements in the list.
Syntax:
list_name.clear()
The below code snippet shows the working of the clear() method:
# creating a list list1 = [1, 2, 3, 4, 5, 6] # removing elements from a list using clear() list1.clear() print(list1)
Output:
[]
3. count(): The count() method returns the number of occurrences of an element in the list. The return value is an integer. The count can be stored to another variable.
Syntax:
variable_name = list_name.count(value)
The below code snippet shows the working of the count() methods:
# creating a list list1 = [1, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8, 9] # checking the number of occurrences using count() a = list1.count(6) print('The number of occurrences of the element in the list is:', a)
Output:
The number of occurrences of the element in the list is: 3
4. reverse(): As the name describes the reverse() method reverses the elements of the list.
Syntax:
list_name.reverse()
The below code snippet shows the working of the reverse() method:
# creating a list list1 = [1, 2, 3, 4, 5, 6] # reversing the list using reverse() list1.reverse() print(list1)
Output:
[6, 5, 4, 3, 2, 1]
5. sort(): The sort() method sorts the elements of the lists. The method sorts the elements in ascending order by default. To sort the elements in descending order we need to pass “reverse = True” as a parameter to the sort() method.
Syntax:
list_name.sort() #ascending order list_name.sort(reverse=True) #descending order
The below code snippet shows the working of the sort() method:
# creating a list list1 = [5, 6, 2, 3, 4, 7, 9, 8, 1] # sorting in ascending order list1.sort() print(list1) print() # sorting in descending order list1.sort(reverse=True) print(list1)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9] [9, 8, 7, 6, 5, 4, 3, 2, 1]
6. max(): The max() method as the name implies returns the greatest or the maximum of all the elements in the list. The maximum can be stored in another variable.
Syntax:
variable_name = max(list_name)
The below code snippet shows the working of the max() method:
# creating a list list1 = [25, 100, 56, 78, 98, 12, 148, 1024] # retrieving the maximum element in the list a = max(list1) print('The maximum element in the list is:', a)
Output:
The maximum element in the list is: 1024
7. min(): The min() method is just the opposite of the max() method. Therefore, it returns the lowest or the minimum of all the elements in a list. The minimum can be stored in another variable.
Syntax:
variable_name = min(list_name)
The below code snippet shows the working of the min() method:
# creating a list list1 = [25, 100, 56, 78, 98, 12, 148, 1024] # retrieving the minimum element in the list a = min(list1) print('The minimum element in the list is:', a)
Output:
The minimum element in the list is: 12
8. sum(): The sum() method returns the sum of all the elements in the list. Therefore, we do not need to use a looping statement to obtain the sum of all the elements in the list. The sum can be stored in another variable.
Syntax:
variable_name = sum(list_name)
The below code snippet shows the working of the sum() method:
# creating a list list1 = [25, 100, 56, 78, 98, 12, 148, 1024] # retrieving the sum of all elements in the list a = sum(list1) print('The sum of all elements in the list is:', a)
Output:
The sum of all elements in the list is: 1541
9. len(): The len() method returns the number of elements in the array as an integer. The returned length can be stored in a variable.
Syntax:
variable_name = len(list_name)
The below code snippet shows the working of the len() method:
# creating a list list1 = [1, 2, 3, 4, 5, 6] # obtaining the number of elements in a list a = len(list1) print('The number of elements in the list is:', a)
Output:
The number of elements in the list is: 6
This is the end of this article. This is the third part of two posts about lists in python. Please refer to the previous articles called Lists in Python | Part 1 and Lists in Python | Part 2 to learn about creating a list, adding elements to a list, accessing elements from a list and list slicing.
Leave a Reply