Lists in Python | Python for Data Science | Day 8
Our agenda for today is – Lists in Python. In Python, whenever we need to store a sequence of values, lists in python are a go-to! They are used to store heterogenous data and are mutable. In this tutorial, we will cover precisely all aspects of lists from creation to deletion. So, follow this article thoroughly to hone your knowledge on lists.
Open your Jupyter notebooks or IDE and type in the code instances below for a better apprehension. So, let’s code it out!
Firstly, let’s learn how to create a list. It’s pretty simple. Just enclose the comma separated list items within square brackets and you’re done. Look at the image below:
Not just integers, a list can comprise of strings, boolean values i.e. True and False, floating point values and even a combination of all these, Refer to the code snippets below:
Like we used list_name[index_no] to access peculiar list items ( see last 3 lines of code in the image above), we can also use negative indexing. Say, if you need to find out the last element of the list but you don’t know its index number, you may straight away use this:
Now, what if you need to find the 1st and 2nd element of a list ? You may use a single line of code to do so.
An important function that we use with lists is the len() function. Precisely, len() allows use to find the length of the list i.e. the no. of elements in the list or simply, list members.
You can also use the function list() to convert into a list. Refer to the code below. Here, we demonstrate how you can use list() function to convert a tuple called my_tuple to a list.
Now, let’s create another list called cars and check if Mercedes is present in it.
To do this, we simply use the “in” keyword in Python. It checks the availability of a specified term in the list and returns True or False accordingly.
Now, I wish to change Range Rover to Maserati. For this, I can simply write the code below:
Let’s change both Audi and Ferrari to Honda & Thar by using only a single line of code.
If you have been asked to add Maruti on the third place in the list of cars, how will you approach the problem? The answer is simple. Use the insert() function.The insert() function allows you to insert an item at a specified location in the list.
Here’s the syntax of insert():
list_name.insert( index, item)
Let’s look at an instance to understand the usage of insert().
Look carefully. After using insert(), Maruti has been added to the third position i.e. the second index and the subsequent list items are shifted by one place.
If you wish to add an item to the end of the list, simply use the append() function.
If you wish to combine two lists together, you can do so by leveraging the power of extend() function. For demonstrating this, let’s create a new list called “bikes” and combine it with cars.
Thus, the two lists got combined and saved in cars. If you wish the two lists combine and get saved in bikes, paste the line below in your notebook.
Let’s see how we can sort the list in alphabetical order using the sort() function.
- In ascending order:
- In descending order: (Just specify the parameter reverse as True):
As we near the end of today’s discussion on lists, let’s get an overview of deletion.
To delete an element from the list, there are certain conditions:
- Deletion by item name:
- Use remove keyword
- Deletion by item index:
- Use del keyword
- Use pop() function
Now that our purpose is complete, let’s delete the entire list now.
Alright fellas! Let’s call it a day. If you need more information on lists, read our tutorial on For Loops or visit: https://www.w3schools.com/python/python_lists_loop.asp
Before you turn to Netflix & Chill, quickly go over a few questions we have curated for you. We have also answered them for your convenience. But as always, attempt them on your own before seeking our help.
We have covered 40+ complete Data Science company interviews from the candidates who cracked these interviews.
Data Science Companies interview questions
We also have 30+ e-books on Amazon, Insta Mojo and books which can be delivered directly on your email address
Complete Set of e-books from The Data Monk
Understand some of the very complex topics in Analytics which are asked in most of the interviews
The Data Monk Top Articles
How to become a Data Scientist? Complete study material, free resources and websites to practice
Become a Data Scientist
Make your profile on our website and practice at least 5-7 questions per day. Be a part of ~2000 Analytics expert
Intern | The Data Monk