Data Structures in Python

Reading Time: 4 minutes

Hello readers, this blog is all about the predefined data structures in python. This includes Lists, Tuples, Sets, and Dictionary. We’ll see each of the data structure one by one and how they are declared. 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.

1) list_1 = [1, 'Yatharth', 'Delhi', 'Knoldus', 24]
2) list_2 = list()
3) list_3 = list(<tuple>)
  1. The first one will create a list of items stored in the variable list_1.
  2. The second one will create an empty list stored in list_2.
  3. The third one will create a list from the existing tuple and store it in list_3.
  4. To verify that, 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

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.

tuple_1 = (1, 'Yatharth', 'Delhi', 'Knoldus', 24)
tuple_2 = ()
tuple_3 = 1, 'Yatharth', 'Sharma', 'Developer', 'Knoldus'
tuple_4 = 'knoldus',
tuple_5 = tuple(<list>)
  1. The first one will create a tuple of items and store it in the variable tuple_1.
  2. The second one will create an empty tuple and stores it in tuple_2.
  3. The third one creates a tuple, which shows that the parenthesis are optional, and store it in tuple_3.
  4. The fourth one creates a single item tuple stored in tuple_4.
  5. The fifth one will create a tuple from an existing list, stored in tuple_5.
  6. To verify that, 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.

set_1 = {1, 'Yatharth', 'Delhi', 'Knoldus', 24}
set_2 = set()
set_3 = set(<list>)
  1. The first one will create a set of unique items and store them in set_1.
  2. The second one will create an empty tuple and store in set_2.
  3. The third one creates a set from an existing list and stores it in set_3.
  4. To verify that, 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.

dict_1 = {'Yatharth':24, 'Akash':23, 'Jatin':19, 'Divyanshu':24}
dict_2 = {}
dict_3 = dict ('Yatharth'=24, 'Akash'=23, 'Jatin'=25, 'Divyanshu'=27)
  1. The first one will create a dictionary and store them in dict_1.
  2. The second one will create an empty dictionary and store it in dict_2.
  3. The third one creates a dictionary from the provided variable list and stores it in dict_3.
  4. To verify that, 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 Trainee 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 C, C++, Java and currently working on DevOps technologies/tools like Ansible, Jenkins, Docker, Kubernetes. Apart from programming, he loves listening to rap music.

Leave a Reply