Python List Methods

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.



Python Append Method


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.


elves = ["Legolas", "Elrond", "Galadriel"]


After this code, as an output we will get:


['Legolas', 'Elrond', 'Galadriel', 'Arwen']


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:


numbers =[10,18,23,45,82]


Here, we add 100 to the existing list numbers.


[10, 18, 23, 45, 82, 100]





You can also check Python Dictionary Methods



Python Insert Method


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.


list = ["hello", "how", "are", "today"]
list.insert(3, "you")


The output will be:


['hello', 'how', 'are', 'you', 'today']




As a second example, we will add a number in a number list.


list = [4,6,7,2,8]


The output of this Python Insert Method example will be:


[4, 6, 7, 10, 2, 8]






Python Pop Method


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.


list = ["hello", "how", "are", "you", "today"]


The output of this python pop method will be :


['hello', 'are', 'you', 'today']



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.


list = [4,6,7,2,8]


The output of this python code will be like below:


[4, 6, 2, 8]



Python Remove Method


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.


list = ["hello", "how", "are", "you", "today"]


The above python code will return as:


['how', 'are', 'you', 'today']




As you can see above, the member “hello” is removed form the list.


The similar example with a number list will be like below:


list = [4,6,7,2,8]


The output of this python remove method example will be:


[4, 7, 2, 8]


As you can see, the member “6” is removed form the list.



Python Extend Method


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.


LoR = ["Legolas", "Elrond", "Galadriel"]
kings = ["Aragorn","Theoden"]


The output of this python command will be


['Legolas', 'Elrond', 'Galadriel', 'Aragorn', 'Theoden']



The same result will be also true for number lists.


numbers = [3,5,7,8]
numbers1 = [32,45,67]


The output of the above python code will be:


[3, 5, 7, 8, 32, 45, 67]



Python Clear Method


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.


elves = ["Legolas", "Elrond", "Galadriel"]


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


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.


numbers = [2,59,6,26,7,12]
x = numbers.index(26)


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.


strings = ["aa","abc","aaa","a","bb"]
x = strings.index("abc")


The output will be 1, because “abc” is the second member of this string list.





Python Reverse Method


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.


LoR = ["Arwen", "Theoden", "Gimli", "Aragorn", "Legolas"]


The output of this Python Reverse method will be:


['Legolas', 'Aragorn', 'Gimli', 'Theoden', 'Arwen']


For a number list, we can also use this python reverse method.


list = [4,6,7,2,8]


The output of this code will be like below in reverse order.


[8, 2, 7, 6, 4]



Python Sort Method


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).


LoR = ["Theoden", "Arwen", "Gimli", "Aragorn", "Legolas"]


The output of this python code will be like below. The members of the lists will be sorted as ascending.


['Aragorn', 'Arwen', 'Gimli', 'Legolas', 'Theoden']




Now, let’s do this sorting as descending (Z to A).


LoR = ["Theoden", "Arwen", "Gimli", "Aragorn", "Legolas"]


The output of this descending sorting will be:


['Theoden', 'Legolas', 'Gimli', 'Arwen', 'Aragorn']


If we use this python sort method with a number list, the number in the list will be sorted smaller to higher.


list = [9,6,4,2,8]


The output of this python code will be:


[2, 4, 6, 8, 9]


Let’s do this sorting as descending:


list = [9,6,4,2,8]


The output of this reverse sort will be:


[9, 8, 6, 4, 2]



Python Copy Method


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.


elves = ["Legolas", "Elrond", "Galadriel"]
characters = elves.copy()


The output will be:


['Legolas', 'Elrond', 'Galadriel']




Let’s show this with another list, a number list this time.


numbers = [10,18,26,34,46,53]
x = numbers.copy()


The output of this python code will be:


[10, 18, 26, 34, 46, 53]



Python Count Method


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.


elves = ["Legolas", "Elrond", "Galadriel"]
x = elves.count("Elrond")


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,


numbers = [4,6,8,9,4,6,7,3,4,5]
x = numbers.count(4)


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.

Back to: Python Programming Course > Python Lists

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course