Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways

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.

Lists in Python

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:

Lists in Python

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.

Lists in Python

Good enough!

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.

bikes.extend(cars)

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.

The Data Monk Interview Books – Don’t Miss

Now we are also available on our website where you can directly download the PDF of the topic you are interested in. At Amazon, each book costs ~299, on our website we have put it at a 60-80% discount. There are ~4000 solved interview questions prepared for you.

10 e-book bundle with 1400 interview questions spread across SQL, Python, Statistics, Case Studies, and Machine Learning Algorithms – Ideal for 0-3 years experienced candidates

23 E-book with ~2000 interview questions spread across AWS, SQL, Python, 10+ ML algorithms, MS Excel, and Case Studies – Complete Package for someone between 0 to 8 years of experience (The above 10 e-book bundle has a completely different set of e-books)

12 E-books for 12 Machine Learning algorithms with 1000+ interview questions – For those candidates who want to include any Machine Learning Algorithm in their resume and to learn/revise the important concepts. These 12 e-books are a part of the 23 e-book package

Individual 50+ e-books on separate topics

Important Resources to crack interviews (Mostly Free)

There are a few things which might be very useful for your preparation

The Data Monk Youtube channel – Here you will get only those videos that are asked in interviews for Data Analysts, Data Scientists, Machine Learning Engineers, Business Intelligence Engineers, Analytics managers, etc.
Go through the watchlist which makes you uncomfortable:-

All the list of 200 videos
Complete Python Playlist for Data Science
Company-wise Data Science Interview Questions – Must Watch
All important Machine Learning Algorithm with code in Python
Complete Python Numpy Playlist
Complete Python Pandas Playlist
SQL Complete Playlist
Case Study and Guesstimates Complete Playlist
Complete Playlist of Statistics

About TheDataMonkGrand Master

I am the Co-Founder of The Data Monk. I have a total of 6+ years of analytics experience 3+ years at Mu Sigma 2 years at OYO 1 year and counting at The Data Monk I am an active trader and a logically sarcastic idiot :)

Follow Me