There are different methods used with lists in python. In other words, there are many Python List Methods. In this lesson, we will focus on these Python List Methods and we will give different examples for each of them. Let’s start to learn medhods of lists in python, python list functions.
Some of the methods used with Python Lists.
Table of Contents
Python append method is one of the Python List Methods. The role of this python method is adding a new member to the python list. Let’s show this with an example.
After this code, as an output we will get:
You can practice below!
You can also watch the video of this lesson!
You can also check Python Set Methods
Python append method can also used with number lists like below:
Here, we add 100 to the existing list numbers.
You can also check Python Dictionary Methods
Python Insert Method is one of the other Python List functions. With this python method, we can insert any member to any location in the list.
Let’s do an example to learn better. Below, we will add “you” string as the fourth member of the srtring.
The output will be:
As a second example, we will add a number in a number list.
The output of this Python Insert Method example will be:
Python Pop Method is used to remove a list member with its index. We use the index number in the Pop Method and then as the output, python code returns the list without that member.
Below, we will remove the second memebr of the list with index 1.
The output of this python pop method will be :
As you can see above, the second member of the list resides at index 1, is removed.
Tın the second example, we will remove the third member of the listt with index 2.
The output of this python code will be like below:
Python remove method is one of the Python List Methods used to remove a specified member from the list. To do this, we use one of the member with this python list function.
The above python code will return as:
As you can see above, the member “hello” is removed form the list.
The similar example with a number list will be like below:
The output of this python remove method example will be:
As you can see, the member “6” is removed form the list.
Python Extend Method is used to ad done list members to another list. With the help of Python Extend Method, we can combine different lists.
Let’s add the ikngs string list to the LoR list with the below example.
The output of this python command will be
The same result will be also true for number lists.
The output of the above python code will be:
Python clear method is another Python List Method. With Python clear method, we remove all the members of an existing list. Below, there is an example for this clear list process.
After thsi command, we will receive an empty list. In other words our list elves will be empty after this python code.
Python Index Method is used to find a member of a list member and return with its index. For example if the member that we are looking for exists as the fourth member of a list, it will return as 3.
Let’s do an example and learn how to use this Python Index Method.
The output will be 3, because “26” is the fourth member of this list, it is index 3.
This is also similar for string lists.
The output will be 1, because “abc” is the second member of this string list.
The other example of Python List Methods is Python Reverse method. The aim of this method is giving the list in reverse order.
We will order the the given list below in reverse order.
The output of this Python Reverse method will be:
For a number list, we can also use this python reverse method.
The output of this code will be like below in reverse order.
Python Sort Method is used to sort the members of a list. By default it is done as ascending (A to Z) because the “reverese” parameter of sort method is False. If we set this parameter as Ture, then the sorting is done through descending (Z to A).
Firstly, let’s do an example with string lists. Below, we will sort our LoR string as ascending (A to Z).
The output of this python code will be like below. The members of the lists will be sorted as ascending.
Now, let’s do this sorting as descending (Z to A).
The output of this descending sorting will be:
If we use this python sort method with a number list, the number in the list will be sorted smaller to higher.
The output of this python code will be:
Let’s do this sorting as descending:
The output of this reverse sort will be:
Python copy method is one of the other Python List Methods. With this command, we can get a copy of an existing list. Below we will get this copy and assign it to a variable. Then, we will print this variable.
The output will be:
Let’s show this with another list, a number list this time.
The output of this python code will be:
Python Count Method is one of the other Python List Methods. With this method, we acn count the number of any members in an existing list.
In the below example, we will check the number of a string in the string list.
The out put of this Python Count Method example will be:
If we do the similar example with a number list that contains repated number,
The output will be 3 like below, because there are three fours in the number list.
In this lesson, we have learned Python List Methods. We have seen what are the common Python List Methods and why we use these built in Python List functions. You can use this methods for different roles and aims in your python codes.
Leave a Reply