Definition of Python Lists Methods
Python list a data structure which contains a collection of values in square brackets that can be muted to our convenience using various methods that are predefined in python programming language and some the methods include a variety of operation from adding values to list, removing or deleting values, slicing a specific value from the list and sorting or rearranging the values in the list to performing numerical operations with the list such as minimum, maximum, summation, etc. In this topic, we are going to learn about python lists methods.
Types of Python Lists Methods
Some of the built-in features or methods that python has for lists are:
- Numerical methods (sum, count, index, min, and max)
We’ll discuss all these types with examples for a clear understanding of these built-in methods in python.
Python list append allows us to add an element or value to the existing list.
lis1=["Horse","Shoe","Turtle",179, 200, 544] lis1.append('snail') print(lis1)
In this example, we have created a list lis1 with numbers and strings and appended the list with a new string value, and the resulting output is the inclusion of the appended element in the list.
Python list insert allows us to insert a value to a specific position in the list.
lis1=["Horse","Shoe","Turtle",179, 200, 544] lis1.insert(3,'snail') print(lis1)
In this example, we have used the lis1 to insert a new string in the 3rd position in the existing lis1 next to the value “Turtle”, and the resulting output is printed.
The Python list extends method allows us to join one or more lists into a new list with an extended version of the given two lists.
lis1=["Horse","Shoe","Turtle"] lis2=[179, 200, 544] lis1.extend(lis2) print(lis1) lis2.extend(lis1) print(lis2)
The two lists lis1 & lis2 is extended either way, and the resulting output, which is the joining of the two lists, is printed.
When we are working with numerical values, in list sum method can be used to get the sum of all elements in the list.
lis2=[179, 200, 544] sum(lis2)
The count method in python gives us the total number of occurrences of a given value inside the list.
lis=["Horse","Shoe","Turtle",179, 200, 544,"Shoe"] lis.count('Shoe')
The element “Shoe” occurs twice in the given list, and hence the count function identifies the exact element and calculates the number of the occurrences of the element “Shoe”, and returns the output.
The Python list index method helps in identifying the index position of an element in the list. It can be given with the start and end parameters when we wanted to narrow down the element or value we are searching for inside the list.
lis=["Horse","Shoe","Turtle",179, 200, 544,"Shoe"] print(lis.index("Turtle")) print(lis.index(179,2,5))
In this example, we find the index value of two elements in the list containing a string and numerical values. In the 1st print statement, we are finding the index of string “Turtle”, which is at the 2nd position in the list, and in the 2nd print statement, we are finding the index value of the number 179, we have given a start and end index parameter from 2 to 5, and resulting output gives us the value 3 which is the position of that value 179. If we give the start and end parameters outside the boundary of the list, then the compiler throws an error.
Min and Max methods allow us to identify the least valued element and maximum valued element in the list.
lis=[179, 200, 544, 11, 18, 2000] print(min(lis)) print(max(lis))
The above example gives us the minimum and maximum value that is available in the list of numerical elements.
The length method in the python list gives us the total length or the total number of characters in the list. It includes all the strings, numbers, special characters, and spaces between the list elements.
lis1=[179, 200, 544, 11, 18, 2000] lis2="Dostoevsky is a legendary writer" print(len(lis1)) print(len(lis2))
In this example, we have declared two lists, one with numerical values and the other one is a statement that contains all string values. In the 1st list, the length function calculates the total elements in the list, which is 6, and in the 2nd list, we have all string values, so all the alphabets and spaces are considered as characters, and we get the total length as 32.
Sort method can be used in both python lists and tuples; its function is to arrange the list or tuple in ascending order. Thus, the sort method lays down the elements in the list in increasing order.
lis1=[179, 200, 544, 11, 18, 2000] print(sorted(lis1))
The list we have given is sorted in the ascending order of the values.
A reverse flag is given to rearrange the values in the list in descending order.
lis1=[179, 200, 544, 11, 18, 2000] lis1.sort(reverse=True) lis1
The reverse sorting is initiated when we give the reverse flag is true.
The pop method in the python list is used to extract a specific element in the list. If the index parameter is not given, then the pop function, by defaults, gets the last element in the list.
lis1=[179, 200, 544, 11, 18, 2000] print(lis1.pop()) print(lis1.pop(2))
The 1st print statement prints the last element in the list since we haven’t declared the index parameter. In the 2nd print statement, we have declared the index 2, and the output pops out the 2nd position in the list.
This method deletes or removes a specific element inside the list, and both delete and remove functions perform a similar operation when declared.
lis1=[179, 200, 544, 11, 18, 2000] del lis1 print(lis1) lis1.remove(11) print(lis1)
The delete method has deleted the given element in index 1, and the remove method deleted the specific value 11 given, which is present in the list.
In this article, we have discussed python list methods in detail using various examples. In addition, we have mentioned the definition of different methods, and examples of those methods were also printed. When working with Lists, the list methods in python will be very useful for performing various operations.
This is a guide to Python Lists Methods. Here we discuss different types of Python Lists Methods along with Examples and their code implementation. You may also look at the following articles to learn more –