Lists in Python (Part-II)

Adhithia
2 min readJun 13, 2021

--

What can we do with lists?

In the Part I of Python Lists we had seen what lists are and what they can hold. Next, it also becomes important to understand the different operations that can be performed on lists that Python provides.

Source TowardsDataScience by Amanda Iglesias Moreno

pop():

The pop() function pulls out the last element of the list. Or it is also safer to say that this function pops out and removes the element that is indicated using its index as a parameter.

Syntax: list_name.pop(index) 
Please note that if index (which is an optional parameter) is not provided, it removes the last element.
List pop Operation

append():

The append() function as the name suggests, is used to append an element to an existing list. Keep in mind that you can append only one element at a time. When you append an element to an existing list, the length of the list will increase by one.

Syntax: list_name.append(element) 
Please note that if the element is a character or a string, place them inside quotes.
List Append Operation

remove():

The list remove() function helps remove an element from an existing list. To be precise, it removes the first matching element that occurs in the list. It is very similar to how the append() function works. The parameter you provide here in the function is the element you want to remove.

Syntax: list_name.remove(element) 
Please note that if the element is a character or a string, place them inside quotes.
Python Remove Operation

At this point, you might be wondering why we need two functions to remove or delete an element — pop() and remove().

The difference between their usages is: In pop(), we ask the function to remove an element by specifying the index at which the element is present. In remove(), we ask the function to remove the element by specifying the element itself.

extend():

The extend() function is used to add multiple elements to the end of an existing list. It iterates through the parameter and adds the elements to the existing list one by one.

Syntax: list_name.extend(iterable_element)
Please note that the parameter we provide here is an iterable element - e.g. could be Strings (which itself is a list of characters) or another List too.
Python Extend Operation

I’m uploading these topics in no particular order. But that’s the end of Lists (Part II). Follow for more operations on lists in (Part III). Also please check out (Part I) here if you haven’t already.

Your comments are valuable. Suggestions to improve content is most welcome!

--

--

Adhithia
Adhithia

Written by Adhithia

MBA student at IIT Kharagpur — VGSOM ’23 | Former Data Scientist at Ashok Leyland | Machine Learning and Artificial Intelligence Enthusiast

No responses yet