Python Data Structures: Built-in tools to manipulate data

We all know that Python is the most popular programming language that has even made kids to start coding in a very easy and fun way. It’s easy and English-like syntax has made it a versatile language which is used in various fields all over the world.

What makes Python versatile is the built-in data structures that are very simple to code and use on any type of data and manipulate the data according to our needs.

First, let’s try to understand What Data structures are?

Data Structures are structures/containers that can store data, organize and manage them, so that it can be accessed and used efficiently. They are basically collection data types.

There are four built-in data structures in Python. They are:

  • List

  • Dictionary

  • Tuple

  • Set

The most commonly used data structures by python developers are lists and dictionaries. Now, let’s see each one of these python data structures in detail:

Data Structure #1: Lists

Python list is a collection of items of any type in a sequential order. A list can have duplicate items as every item is accessed using the index and also the items can be accessed in reverse order by using negative indexing. The lists are mutable, which means that items can be added, removed or changed even after it is created. A list can also have another list as an item in it.

Creating a list:

A list can be created by enclosing elements within [square] brackets and each item is separated by a comma. A shopping list can be an example for lists. Syntax to create a list is:

#Creating a list 
fruits = ['Apple', 'Banana', "Orange"]
print(type(fruits)) #returns type
print(fruits) #prints the elements of the list
Output:
    <class 'list'>
    ['Apple', 'Banana', 'Orange']

Accessing a list:

We can access the items in the list using indexing. Every item in the list has an index associated to it depending on the position of that item in the list. The syntax to access an item in the list:

#Access elements in the fruits list
fruits = ['Apple', 'Banana', "Orange"]
print(fruits[0]) #index 0 is the first element
print(fruits[1])
print(fruits[2])

Output:
    Apple
    Banana
    Orange

But, indexes doesn’t have to be positive always . If we want to access the list from backwards, that is in reverse order, we can use negative indexes as below:

#Access elements in the fruits list using negative indexes
fruits = ['Apple', 'Banana', "Orange"]
print(fruits[-1]) #index -1 is the last element
print(fruits[-2])
print(fruits[-3])
Output:
    Orange
    Banana
    Apple

If we have to return the elements in a range between two positions in a list, we use slicing. We have to specify start index and end index to get a range of elements from a list. The syntax is List_name[start:end:step]. Here, step is the increment value and by default it is 1.


#Accessing range of elements using slicing
fruits = ['Apple', 'Banana', "Orange"]

fruits #all elements 
['Apple', 'Guava', 'Banana', 'Kiwi'] #output

fruits[::1] #start to end with step 1
['Apple', 'Guava', 'Banana', 'Kiwi'] #output

fruits[::2] #start to end with step 2 basically index 0 & 2
['Apple', 'Banana'] #output

fruits[::3] #start to end with step 2 basically index 0 & 3
['Apple', 'Kiwi'] #output

fruits[::-1] #start to end with step 2 - reverse order
['Kiwi', 'Banana', 'Guava', 'Apple'] #output

Adding elements to a list:

We can add items to a list using the append(), extend() and insert() functions.

  • The append() function adds the element to the end of the list.

  • The insert() function adds an element to a specific position in the list which is specified along with the value

#Adding elements
fruits = ['Apple', 'Banana', "Orange"]
#Append new elements
fruits.append('Kiwi')
print(fruits)

Output: 
    ['Apple', 'Banana', 'Orange', 'Kiwi']
#Insert elements in to the list
fruits.insert(1,'Guava') #inserts Guava as second element is the list since the index is specified as 1
print(fruits)

Output: 
    ['Apple', 'Guava', 'Banana', 'Orange', 'Kiwi']

Deleting elements from a list:

Similar to adding elements, removing/deleting elements from a list is also very easy and it is achieved using del(), remove() and pop() methods. To clear the entire list clear() function can be used.

  • del() function deletes element at the given index

  • pop() function removes the element from the list at a given index and also you can assign the deleted element to a variable. If the index value is not specified, then it removes the last element in the list.

  • remove() function removes an element by its value.

  • clear() function empties the list.

#Deleting elements from the list
fruits = ['Apple', 'Guava', 'Banana', 'Orange', 'Kiwi']

#del() function
del fruits[3] #delete element at index 4
print(fruits)

Output