Python List Methods

python-list-methods-www.ipcisco.com

There are different methods used for list 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.

 

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"]
elves.append("Arwen")
print(elves)

 

After this code, as an output we will get:

 

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

 

 

python-list-methods-append-method-ipcisco

 

You can also watch the video of this lesson!

Python append method can also used with number lists like below:

 

numbers =[10,18,23,45,82]
numbers.append(100)
print(numbers)

 

Here, we add 100 to the existing list numbers.

 

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

 

python-list-methods-ipcisco

 


 

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")
print(list)

 

The output will be:

 

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

 

python-list-methods-insert-method-ipcisco

 

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

 

list = [4,6,7,2,8]
list.insert(3,10)
print(list)

 

The output of this Python Insert Method example will be:

 

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

 

python-list-methods-ipcisco-2

 

 


 

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"]
list.pop(1)
print(list)

 

The output of this python pop method will be :

 

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

python-list-pop-method-ipcisco

 

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]
list.pop(2)
print(list)

 

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"]
list.remove("hello")
print(list)

 

The above python code will return as:

 

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

 

python-list-remove-method-ipcisco

 

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]
list.remove(6)
print(list)

 

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"]
LoR.extend(kings)
print(LoR)

 

The output of this python command will be

 

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

 

python-extend-list-method-ipcisco

The same result will be also true for number lists.

 

numbers = [3,5,7,8]
numbers1 = [32,45,67]
numbers.extend(numbers1)
print(numbers)

 

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"]
elves.clear()
print(elves)

 

After thsi command, we will receive an empty list. In other words our list elves will be empty after this python code.

 

[]

python-list-methods-remove-ipcisco


 

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)
print(x)

 

The output will be 3, because “26” is the fourth member of this list, it is index 3.

 

3

python-list-index-method-ipcisco

This is also similar for string lists.

 

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

 

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

 

1

 


 

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"]
LoR.reverse()
print(LoR)

 

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]
list.reverse()
print(list)

 

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

 

[8, 2, 7, 6, 4]

python-list-reverse-method-ipcisco


 

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"]
LoR.sort()
print(LoR)

 

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']

 

python-list-sort-method-ipcisco

 

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

 

LoR = ["Theoden", "Arwen", "Gimli", "Aragorn", "Legolas"]
LoR.sort(reverse=True)
print(LoR)

 

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]
list.sort()
print(list)

 

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]
list.sort(reverse=True)
print(list)

 

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()
print(characters)

 

The output will be:

 

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

 

python-list-methods-copy-method-ipcisco

 

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

 

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

 

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")
print(x)

 

The out put of this Python Count Method example will be:

 

1

 

python-list-count-method-ipcisco

 

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)
print(x)

 

The output will be 3 like below, because there are three fours in the number list.

 

3

 

python-list-count-method-ipcisco.com

 

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 *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k