Python List Methods - append(), sort(), remove(), reverse(), etc. ~ Your Own Linux..!
Hello readers! This is the 9th article of our Python tutorial series 'Python on Terminal' and we will be learning about List Methods in Python in this tutorial. In the previous article on Python Lists - Python Lists Creation, Concatenation, Repetition, Indexing and Slicing, we studied how to create lists, how two lists can be merged and repeated and how indexing and slicing works for Python lists. In this article, we will be learning about various methods associated with Python lists along with examples.
In the last article on Python Lists, we have already seen that, like everything in Python, a list is also an object. It is an instance of the Python class named list. Thus, every list in Python is associated with a number of methods, which when called upon a list object (list_object_name.method_name()), do some processing on list object and returns another Python object (not necessarily a list type only).
So, what all methods can we call on a Python list object? You can get the answer by passing a Python list as an argument to Python built-in dir() function. Lets create a list then and check this.
Again, as we've seen earlier in case of Python strings, list are also associated with 'Magic Methods' (the ones starting with double underscores, e.g. __add__ method), those Python uses internally. Let us now learn some important list methods in detail.
This method appends (inserts at the end) an element (a Python object) to the Python list. To check this, we need to create a list and check how this method works.
The later option - it will append. There is a method extend() which does the concatenation.
This method counts the number of occurrences of an element in the list which the method is called on. So, myList.count('Foo') will count how many times the string Foo has occurred in the list object myList.
This method takes an iterable (always remember this) and appends each item to the list. Thus, if we call this method on myList with another list (which is an iterable) as an argument, all the elements will be appended to myList.
Please observe the last case very carefully. We have provided a string 'Ten' to the extend() method, expecting that the string object 'Ten' will be appended to myList. But, extend() accepts iterables and a Python string being an iterable, all the characters are appended to myList one-by-one and we can see the result.
This method is similar to the string index() method. It accepts an element as an argument and returns the index of the first occurrence of the element in the list, if it is present in the list, otherwise it will give out an error.
The append() method will always add an element to the last position of the list, whereas insert() method accepts an extra argument denoting the 'offset' at which the element has to be inserted.
This method when used without any argument, returns the last element in the list. It does accept an optional argument with which we can specify the offset of the list element to be returned. When the element is returned, it is also removed from the list.
Using pop() method, we can remove an element from a list using it's offset, while with remove() method, we can remove an element from a list using it's value. pop() method with no argument removes last list element, but this won't work in case of remove() method. We must provide the value of the element that needs to be removed.
# It takes exactly one argument !
This method when called on a list object, it reverses it in place, without returning anything. It means that, the contents of the original list are reversed and list is modified. In this context, let me also mention the built-in list reverse iterator object reversed, that returns a reversed list object, with the original list not getting modified.
This method when called on a list object, orders the list elements in ascending order by default, original list is modified in-place. This method can take arguments so as to change the behavior of the default sorting process.
With this, we have learned all the important list methods along with some examples. Python lists are very powerful and useful data structures and you would use them most of the times. So, it is very essential to know all the methods associated with them, study their behaviors and use them accordingly. Thank you for stopping by and going through this article. Please do share your views and opinions in the comment section below and stay tuned for more articles.