Data Structures in Python

Reading Time: 3 minutes

Hello readers, this blog is about predefined data structures in python. This includes Lists, Tuples, Sets, and Dictionary. We’ll see each of them one by one with their declarations. So, let’s get started.

Lists

A List is a data structure which has an ordered sequence of elements. The elements in the list can be mutable, which means that we can add, remove and modify the value of existing elements. Lists can contain any type of data, i.e. they are heterogeneous.

To declare a list, we need to put items in between [ ]. Let’s see various ways of how we can declare a list.

## Create a list of items
list_1 = [1, 'Yatharth', 'Delhi', 'Knoldus', 24]

## Create Empty List
list_2 = list()

## Create a list from existing tuple
list_3 = list(<tuple>)

To verify the type, we can use the type()method as follows:

type(list_1)

Output:
<type 'list'>

As the list is an ordered sequence of elements, we can access the elements of the list by their index value starting from 0 to count-1. We can manipulate these values through their index value. Another way, python uses for indexing is negative indexing in which the count is provided from last to first starting from -1. Here’s a table to show how the indexing for each value will be done.

Elements: 1 ‘Yatharth’ ‘Delhi’ ‘Knoldus’ 24
Positive Index: 0 1 2 3 4
Negative Index: -5 -4 -3 -2 -1
Indexing

Tuples

A Tuple is a data structure which has an ordered sequence of elements. The elements in the tuple are immutable, which means that we cannot add, remove or manipulate the elements in the tuple. Tuple can contain any type of data, i.e. they are also of heterogeneous type. Tuples are more efficient than List.

To declare a tuple, we need to put items in between ( ). Let’s see various ways of how we can declare a tuple.

## Create a tuple with defined items
tuple_1 = (1, 'Yatharth', 'Delhi', 'Knoldus', 24)

## Create an empty tuple
tuple_2 = ()

## Create a tuple without parathesis (they are optional)
tuple_3 = 1, 'Yatharth', 'Sharma', 'Developer', 'Knoldus'

## Single item tuple
tuple_4 = 'knoldus',

## Tuple from existing list
tuple_5 = tuple(<list>)

To verify the type, we can use the type()method as follows:

type(tuple_1)

Output:
<type 'tuple'>

Same as List, the Tuple is also an ordered sequence of elements, we can access the elements of the list by their index value. The concept of negative indexing is also applicable here.

Set

A Set is a data structure which has an unordered collection of elements. The elements in the sets are mutable. The main characteristic of a set is that it cannot have duplicate elements. A set can also be immutable, by declaring a Frozen Set. A Set can also contain any type of data, i.e. they are also of heterogeneous type. Set is much optimized for checking if the element is available in the set.

To declare a Set, we need to put items in between { }. Let’s see various ways of how we can declare a set.

## Create a set of unique items
set_1 = {1, 'Yatharth', 'Delhi', 'Knoldus', 24}

## Creates and empty set
set_2 = set()

## Create a set from existing list 
set_3 = set(<list>)

To verify the type, we can use the type()method as follows:

type(set_1)

Output:
<type 'set'>

The set, being an unordered collection, cannot be accessed by the index value.

Dictionary

A Dictionary is a data structure which maps the keys to values. The keys and values comprises of any immutable data type. In Python version 3.5 and earlier, the dictionary data type is unordered. However, in Python version 3.6 and later, the dictionary data type remains ordered.

To declare a Dictionary, we need to put the keys and value in between { } which are separated by :. So basically the syntax looks like {'key':'value'}. Let’s see various ways of how we can declare a dictionary.

## Create a dictionary with defined values
dict_1 = {'Yatharth':24, 'Akash':23, 'Jatin':19, 'Divyanshu':24}

## Creates an empty dictionary
dict_2 = {}

## Creates a dictionary from provided variables
dict_3 = dict ('Yatharth'=24, 'Akash'=23, 'Jatin'=25, 'Divyanshu'=27)

To verify the type, we can use the type()method as follows:

type(dict_1)

Output:
<type 'dict'>

As Dictionary stores the key : value pair, to access its elements, we cannot use the index value, instead, we provide the key to fetch the value mapped to that key as: variable['key'].

So as you have reached so far, you’ll now have knowledge of the various data structures used in python. Still, if you have any queries, feel free to contact me at yatharth.sharma@knoldus.in.

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies.

Written by 

Yatharth Sharma is a Software Consultant at Knoldus Software LLP. He has done MCA from Bharati Vidyapeeth Institute of Computer Application and Management, Paschim Vihar. He has a decent knowledge of Java Language and currently working on DevOps technologies/tools like Ansible, Jenkins, Docker, Kubernetes. Apart from programming, he loves listening to rap music.