Python list methods

List in Python is a collection of multiple data stored in a variable. Python list is of string, integer or a combination of string and integer data type.

Method in python is a piece of code that executes a task on an object when it is called upon. Python list methods make working on list a lot easier and efficient.

Most commonly used Python list method are :

append() reverse() pop() index() insert() sort() count() clear()

Before we start discussing the role of each method, let's start by creating a Python list.

Screenshot (11).png

Starting with the append() method. The append() is used to add or "append" a new element at the end of the list elements. let's add Banana to our fruits list.

append.JPG

The reverse() method as the name indicates is used to reverse the ordering of the list elements.

reverse.JPG

The pop() method deletes an element from the list. The pop() methods takes one argument which is the index of the list element that gets deleted from the list, if there is more than one argument an error is thrown up.

If there is no argument pop() method deletes the last element in the list by default.

pop.png

Index() methods returns the position of a given element in the list. if an element appears more than once in the list, the index of the first occurrence is returned.

If the element is not found a " ValueError" exception is raised.

Screenshot (14).png

Sometimes while working with a list you would love to get inputs from users and store them in a list, insert() methods helps you do that. although append() can be used to add an element to the list, however unlike insert() method append() only add a new element to the end of the list.

insert() method takes two arguments. The first argument indicates the index where the new element would be inserted, second argument is the new element to be inserted in the list.

Screenshot (15).png

In some cases a list would need to be sorted before it would be worked on. For example when building a program that performs binary search the list must be sorted for the binary search operation to be performed efficiently.

sort() method performs the sorting operation and returns a sorted list. sorting can either be in ascending or descending order. when there is no argument sorting is done in ascending order.

Screenshot (17).png

To sort a list in descending order sort() method takes an argument " reverse" which is set to True.

Screenshot (16).png

To know the number of times an element appears in a list, count() method is used to perform this task. An argument is passed to the count() method which is the item whose number of occurrence is checked.

Screenshot (19).png

The clear() methods deletes all the items on the list and returns an empty list.

Screenshot (21).png

Conclusion, Python list methods are very useful and handy when working on a list. Today we discussed basic functions of Python list methods and I hope you have learned something valuable from this article.

Follow me on Twitter: @minospeed007 for more articles on tech.