Data Structures in Python.

Vinodnethichinna
Analytics Vidhya
Published in
6 min readAug 9, 2020

I remembered the day when I made up my mind to learn python then the very first things I learned about data types and data structures. So in this article, I would like to discuss different data structures in python.

So initially, what is data structures? in a simple world, it is a structure that can hold data, where data refers to a different type of data and related data. overall it is the concept of organizing and storing data so it can be accessed easily and work efficiently.

The different types of data structures available in Python are listed below.

  1. List
  2. Tuple
  3. Set
  4. Dictionary

We will start with List and see the different operations we can do with List. The List is a Mutable data structure in Python that means after the list is created we can perform data manipulations on it like the update, delete, insert operations.

We can simply create a list of numbers with below syntax.

Not only numbers we can store string, decimals in the list. we can modify the list also.

So, we are able to store data in a list, now the question comes how to access those elements of data?.

We can access those elements by their index number. To fetch the first element of data we need to use the below syntax.

Similarly, we can get the second, the third element as required. Now we want to see the last element in a list. We can do this in two methods.

The first method is using the length method to identify the length of the list.

Using length we can find the last index number by subtracting 1 from the length.

See the last element in our myList is extracted. Now we will try with the second method. we need to add “-” (Minus) symbol before index number so we are reading list from the reverse, So we can access the last element by using index number as -1.

Similarly, we can extract the last second or last third number accordingly by changing the index numbers with -2 and -3 respectively.

What if we need to update the list, Here we go. We are going to change the value of 3rd element from “ramu” to “Krishna”.

see that is simple we can change the list accordingly. Now we add new values to list. To achieve this we will use the append method.

See we have added new element “Vinod” to list. Now we will see how to delete elements from the list. Again we can do this in 2 ways. The first way is to use the remove method. we need to give the value of elements to be removed. For example, we need to remove the “Vinod” element we just added.

Note: Make sure you enter the data value correctly. It is case sensitive and keeps an eye on lowercase and uppercase letters.

The second way of removing elements is by using the pop method. by default, it will remove the last element in the list.

Now we will see the extend method in List

we can see a new list named myList2 is added to myList at the end. so using this we can add two lists.

Slicing in List

We can slice the list and able to take the required part of the list for our operations. This will come in handy when we prepare our data for machine learning algorithms.

Here is the syntax of using slicing operator.

ListName[startindex:endindex:step]

By default step size is 1 and we can change it as per our requirements. we will see a few examples of using this operator.

Note: The upper boundary is excluded so please keep in mind before giving value to the end index.

We can access the list in reverse order and using the below code we can reverse the list in one line of code.

if we skip the values in start index and end index default values are taken like 0 for start index and length of the list in end index. we gave -1 in step so list starts reading from right to left.

Reverse Method

We can reverse the list using the reverse method. we will make a list and try to use the reverse method and print the list again. now we can see our elements in list are in reverse order.

List Copying

We can copy the list in 2 different ways. Technically speaking they are shallow copy and deep copy. We will see the difference between them.

Shallow Copy.

In this, we will create another list but the problem is both lists are pointing to the same memory location. any change in the one list will reflect in both the lists. so we are not maintaining a copy of the list but we are having one list with two different names. Please see the below code.

hereafter creating otherList, we tried to change the 2nd index element to “Cat”. we saw this by printing otherList, all good till here. Now we are printing the actual List myList, even here we can see the “Cat”, we lost “Apple”. so this is not a copy but 2 different names for one list, both the lists are pointing to the same memory location. To overcome this problem and able to store the previous list we have to go for Deep Copy.

Deep Copy.

In this case, we are having a copy of list, they are not linked to each other and they are pointing to different memory locations. so any change in one list will not affect other lists, doing this we ensure no data loss.

Now we tried to change the element in otherList, but it doesn’t affect myList. Doing this we can have a copy of List.

By this, we came to the end of the List data structure and methods we can use on it. In my next article, we will discuss other data structures in python like Tuple, Set, Dictionaries, Strings.

Please feel to share or comment if there are any mistakes/queries. I hope you all enjoyed reading this article. Please do mention if went wrong somewhere, I am still learning and suggestions will improve my articles.

Thank you.

Stay Safe.

--

--

Vinodnethichinna
Analytics Vidhya

Technical, Enthusiastic and Organized Post Graduation Student with great attention to detail and analytical skills.