More about Lists in Python
Understanding append, extend, count, remove, index, pop, insert, sort, and reverse functions in Python.
Hello, Python Enthusiasts!
In the last section of this Python Tutorial Series, We talked about Python Lists and understood them with some examples. In this article, we will go a step further. We will add some built-in Python functions to the Python List. It is an easy concept but very helpful if you are working on some database projects or any other python based project.
Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad
insert() Function
The insert Function is very helpful in adding an element to a specific index in the list. You can do that using the following way:
>>> players = ['Dhoni', 'Kohli', 'Raina', 'ABD', 'Sachin']
>>> players.insert(2, 'Rohit')
>>> players
['Dhoni', 'Kohli', 'Rohit', 'Raina', 'ABD', 'Sachin']
You can see that “Rohit” is added to index 2 in the player's list. The 2nd index position of “Raina” is shifted to 3rd and so on.
reverse() Function
The function reverse reverses the order of the element in a list. For example:
>>> players = ['Dhoni', 'Kohli', 'Rohit', 'Raina', 'ABD', 'Sachin']
>>> players.reverse()
>>> players
['Sachin', 'ABD', 'Raina', 'Rohit', 'Kohli', 'Dhoni']
sort() Function
You can use the sort Function to arrange the list in ascending order. For example:
>>> players = ['Dhoni', 'Kohli', 'Rohit', 'Raina', 'ABD', 'Sachin']
>>> players.sort()
>>> players
['ABD', 'Dhoni', 'Kohli', 'Raina', 'Rohit', 'Sachin']
reverse() Function
Sometimes we wish to arrange the elements of a list in descending order. You can do this in the following way:
>>> players = ['Dhoni', 'Kohli', 'Rohit', 'Raina', 'ABD', 'Sachin']
>>> players.sort(reverse=True)
>>> players
['Sachin', 'Rohit', 'Raina', 'Kohli', 'Dhoni', 'ABD']
append() Function
The function append inserts the object at the end of the list. For example:
>>> players = ['Dhoni', 'Kohli', 'Rohit', 'Raina', 'ABD', 'Sachin']
>>> players.append('Warner')
>>> players
['Dhoni', 'Kohli', 'Rohit', 'Raina', 'ABD', 'Sachin', 'Warner']
list() Function
The function list takes a sequence as an argument and returns a list. For Example:
>>> name = 'Rohit'
>>> list(name)
['R', 'o', 'h', 'i', 't']
extend() Function
The function extend accepts a sequence as an argument and puts the element of the sequence at the end of the list. For example:
>>> x = [5,15,25]
>>> x.extend('ABCD')
>>> x
[5, 15, 25, 'A', 'B', 'C', 'D']
count() Function
This function counts the total number of times an object passed as an argument in the list. For example:
>>> a = [10, 20, 30, 40, 30, 50, 60, 30, 30, 70, 90, 30, 80]
>>> a.count(30)
5
pop() Function
This function returns the element from the list whose index is passed as an argument. This function removes the poped element from the list. For example:
>>> a = [10, 20, 30, 40, 30, 50, 60, 30, 30, 70, 90, 30, 80]
>>> a.pop(4)
30
remove() Function
The remove Function removes the first encountered element of the passed argument. For example:
>>> a = [10, 20, 30, 40, 30, 50, 60, 30, 30, 70, 90, 30, 80]
>>> a.remove(30)
>>> a
[10, 20, 40, 30, 50, 60, 30, 30, 70, 90, 30, 80]
del Operator
The del operator is used to remove a subsequence of elements. For example:
>>> a = [10, 20, 30, 40, 30, 50, 60, 30, 30, 70, 90, 30, 80]
>>> del a[2:7:2]
>>> a
[10, 20, 40, 50, 30, 30, 70, 90, 30, 80]
That’s it for this article. I hope I covered all the topics related to the Python List. If I missed something then let me know in the comment section.
In the next article, we will look after how the built-in functions perform on a list.
If this article sounds informative to you then make sure to follow and clap. Share it with your geek community.
Thanks for reading.
Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).
More content at plainenglish.io. Sign up for our free weekly newsletter here.