Five Most Useful List Methods in Python

Intro

List methods are useful, and we have to use these most of the time when working with List. There are more than five methods. But I will write about the five most useful methods that I have used the most.

Python List is equivalent to Array. So, if I use Array word instead of List, you can assume that both are the same.

append()

This is the very first method that uses for adding new value to a list. It takes a value as a parameter and appends the value to the end of the list. Pretty useful, right?

Let’s see an example.

programming_language = ['Python', 'Java', 'C', 'Go']
print(programming_language) # ['Python', 'Java', 'C', 'Go']

programming_language.append('JavaSrcirpt')
print(programming_language) # ['Python', 'Java', 'C', 'Go', 'JavaSrcirpt']

extend()

This is another useful method when we need to add a new list with the current list. We are not limited to pass a list, we can also pass strings, tuples, etc. Actually, it takes an iterable as a parameter. And then, the given iterable will add with the end of the current list.

Let’s see some examples.

list1 = ['A', 'B', 'C']
list2 = ['X', 'Y', 'Z']

print(list1) # ['A', 'B', 'C']

list1.extend(list2)

print(list1) # ['A', 'B', 'C', 'X', 'Y', 'Z']

list1.extend('Python')

print(list1) # ['A', 'B', 'C', 'X', 'Y', 'Z', 'P', 'y', 't', 'h', 'o', 'n']

insert()

Generally, the append method added a value to the end of a list. But using the insert method, we can add at any position of a list.

It takes two parameters, the first one is for which position we want to put the value, and the second one is for the value. Let’s see some examples.

programming_language = ['Python', 'JavaScript']
print(programming_language) # ['Python', 'JavaScript']

programming_language.insert(1, 'PHP')
print(programming_language) # ['Python', 'PHP', 'JavaScript']

programming_language.insert(0, 'C')
print(programming_language) # ['C', 'Python', 'PHP', 'JavaScript']

We can get the index of a value by passing it in the index() method.

pop()

This method is used to remove an element from a list. We can pass an index as a parameter to remove a specific element.

If we use this without passing any parameter, it will remove the last element from the list, And return it. Let’s see some examples.

programming_language = ['C', 'Python', 'PHP', 'JavaScript', 'C++']
res = programming_language.pop(2)
print(res) # PHP
print(programming_language) # ['C', 'Python', 'JavaScript', 'C++']


res = programming_language.pop()
print(res) # C++
print(programming_language) # ['C', 'Python', 'JavaScript']

There is another similar method called remove() to remove an element from a list. It takes an element as a parameter, not its index. If there is more than one similar element, then this method will remove the very first one. Let’s see some examples.

programming_language = ['C', 'Python', 'Python', 'PHP', 'JavaScript', 'JavaScript', 'C++', 'Python']
programming_language.remove('C++')
print(programming_language) # ['C', 'Python', 'Python', 'PHP', 'JavaScript', 'JavaScript', 'Python']


programming_language.remove('Python')
print(programming_language) # ['C', 'Python', 'PHP', 'JavaScript', 'JavaScript', 'Python']

clear()

The clear method is pretty simple. It removes all the elements from a list and makes it clear. Pretty self-explanatory. Let’s see the following code.

programming_language = ['C', 'PHP', 'JavaScript', 'C++', 'Python', 'Java', 'Go']
print(programming_language) # ['C', 'PHP', 'JavaScript', 'C++', 'Python', 'Java', 'Go']

programming_language.clear()

print(programming_language) # []

Conclusion

Using some of these methods, we can build stack and queue data structures easily. We don’t need to write any extra code except these codes.

I hope you got a good idea about these methods. In addition to these, some other methods are not covered in this blog. The two most notable of these are, reverse() and sort(). I will write about these two methods in a separate blog.

Scroll to Top