# List | What kind of data structure is a list?

## Target

• The List is an integral part of Python when it comes to data structure..
• Understand how to perform different operations on a list.

## Introduction

Let's imagine a scenario, we are required to store the grades of all students on the 10th reading of the class. One way to solve this problem is to use variables for all students and store their grades. As shown below.

Note: If you are more interested in learning concepts in an audiovisual format, we have this full article explained in the video below. If that is not the case, you can keep reading.

But this is not an efficient way to solve our problem like

• The number of variables becomes very large as the number of students increases. This can cause a large memory overhead.
• We will not be able to analyze the data to answer some questions such as what are the average grades of the last five students?? We cannot answer these questions as the variables do not inherently support the sequence.

Then, we need a data structure that supports sequences and helps in data related questions. This is where the list comes in.

1. What is a list?
2. Subset
3. Add items to an existing list
4. Remove items
5. Cycle through a list to access its items.

Let us now analyze each of them in detail.

## What is a list?

A list is an ordered data structure with items separated by a comma and enclosed in square brackets.. For instance, list1 and list2 shown below contain only one data type.

Here, list1 has integers while list2 has strings. Lists can also store mixed data types as shown in list3 here.

## Subset

Now let's see how we can access the elements of a list. Before creating a subset, let's see how elements are stored in it. Let's take the example of the list, its elements are stored using indexes from 0.

Here, the element 1 It's in 0 and the element 2 in the index 1, and so on.

If you want to extract a single item, let's say in the index 1 we can use the bracket together with the index number. Then, list3[1] will give the “Python”, which is present in the index 1. A) Yes

This is how we can extract a single item from a list, and if we want to extract a sequence from it. We can do this using the following format, where inside a bracket we can give a range separated by a colon.

Been and, list3[1:4], will start from index 1 and will hit an index before 4. That means the list starts from 1 and stops at 3, does not include 4. Similarly, yes it gives list3[2:5] will return a list of items in list3 from index 2 al 4.

Negative indices are another interesting concept. Suppose you want to access the last element of it, you can use list3[-1]. It will give you the last item in the list, which is impressive in this case.

## Add items to an existing list

Now let's explore how we can add an item to an existing list. A single item can be added using an add function. As shown below.

In this example using append, we have added the element 4 in the end.

We can also add multiple items to the list, for this we use the extend function. The extend function accepts multiple elements, compared to add, that accepts a number, namely, only one element. Look at the example below-

Here, the function will extract each item from the input list and add it to the main list, what is list3 in this case.

We can also add a list to the existing one. For this, we can use the add function as discussed above. The only difference is that instead of a number we will pass lists as input. Will add the entire input list as one item.

Then, in this example, the input list [7,8] is added in the sixth index of the list3. What's more, can be accessed by index value.

## Remove items

Let's see how we can remove items from a certain list. Suppose you want to remove an element by its value. In this case, we can use the delete function and give the element we want to delete as input. For instance, if we want to delete 2 as it's shown in the following, we can use the code list3.remove (2). when i print the list, there will not be 2 present in it.

The second method is when we use the index value to remove the element. In that case, we will use the keyword of. Suppose I want to delete the element present in the index 3, will wear from list3[3]. As shown in the following picture

## Loop over list

Let's see how you can iterate over each item in a list to perform a task like print. Here is an example of how we can iterate over it using a For loop to print the elements.

The For loop will extract each element and print it. This is how we can access each element through a 'for' loop.

## Final notes

This was all about Lists in this article. Here, we saw what it is, how to access elements of it. What's more, we saw how to add and remove items along with how to iterate over them to process them further.

If you are looking to start your data science journey and want all topics under one roof, your search stops here. Take a look at DataPeaker's certified AI and ML BlackBelt Plus Program

If you have any question, let me know in the comment section!