You are currently viewing Data Structure in Python || List || Tuple || Set || Dictionary
Data Structure in Python

Data Structure in Python || List || Tuple || Set || Dictionary


Firstly we need to understand the data structure and then we will know how it is implemented in python. We will start from very basics and will cover the complete data structure of Python.

Data Structure introduction:

Data structure is a vast field of computer science in which arrangement of data is defined. It perform many task to provide efficient way for manipulation, operation, modification and access of data.

The main part cover by the data structure is:
1. Data Management: Management way of the data in storage device. It is responsible for easy data storage and retrieval, Provide security of multiple users etc.

2. Data Organisation: Classification and arrangement data to make it easy to use.
3. Data storage: Storage of data in volatile, non-volatile and semi-volatile disk in a efficient manner.

Python also provide some type of data structure to make its interpretation easy.
Note: Python is officially interpreted language but many other system of python IDE provide compilation feature.

In easy language data structure is also known as data-type in computer science with upper level abstraction view. Obvious data structure is a separate field which is responsible to arrange all type of data. But in programming sense we can understand, the structure of data by their type. data-type is reflaction of data structure.

For example:

Here “a” is a variable and contain the assigned integer value 5. It means we are defining a variable of integer type which will occupy the memory in storage disk with a particular physical and logical address. Further, on calling of variable, it will refer to assigned address and fetch the stored value. Name of variable is abstraction of address of particular memory address. Apart form this side we will focus on main part of this tutorial i.e, data-type.

In python you can directly assign variable with any data-type. Their is not any requirement of initialization. In general term this feature is known as dynamic typing.
Let me clear one thing also that, in python there is not any types of variable. The label of value which is associated that variable become the data-type of that variable.

For example:
Here we have been directly initiated a variable ”text”. Python interpreter will automatically understand that “text” is a sting type variable.

Now we will understand each data structure with example as shown in tree structure image.

Data Structure in Python
Data Structure in Python

Primitive data-type:

The most basic type of data is known as primitive data-type. Such type of data-type cant be separated further. For example numbers and string.

a= 5


Boolean is a very common data-type used in mostly all type of computer programming language. This data-type contain only two values, True or False (1 or 0).This data-type is used in conditional operator to check that condition is True or False.

Lets understand this with the help of example.

if barrier==True:
print(“Welcome to Aisangam”)
print(“Condition is False”)

In this code snippet barrier is a variable containing boolean value. Further using “if” statement it is checked that condition is true or false. Because here condition is True so it will print “Welcome to Aisangam”.

You can also perform mathematical operation on boolean data-type. It acts like other number data-type.

a= True+5
Print (a)
Output: 6

a= False+5
Print (a)
Output: 5

here True work as 1 and false work as 0.


String is also a most common primitive data-type used in mostly all programming languages. In python any number, character, symbol enclosed by single (‘’)or double (“”) quote is interpreted as string data-type. String is a immutable (fixed) data-type. Immutable data-type means the variable is allocated memory only once at the time of its declaration. Further it can be only re-use in through-out the program.
For Example:

text= “This is Aisangam Tutorial”
Print (text)
Output: This is Aisangam Tutorial

Further, if you have to print text in multiple line then you can enclose text with three double quote.

For example:

text=”””This is Aisangam tutorial.
Aisangam welcomes you.”””
print (text)

This is Aisangam tutorial.
Aisangam welcomes you.

String works like list in the way of slicing. Slicing means you can get only certain part of string from their index value.

text= “This is Aisangam Tutorial”
Print (text[8:])

output: Aisangam Tutorial

text[8:] this type of text separation is known as slicing. it is defined like
[start_Index:end_Index] type of indexing. By default it start with 0 index and end with last index.

In depth when you will work with different language-type or format then there will be great problem of encoding. In python there is some key confliction which have to know. It will help you to solve many challenges in future.

In python 2.7

print(text, type(text))

print(text, type(text))

Aisangam, <type ‘str’>
Aisangam, <type ‘unicode’>

In python 3.5

print(text, type(text))

print(text, type(text))

Aisangam, <type ‘str’>
Aisangam, <type ‘str’>

From this code part we got that python2.7 support both encoding(Non-unicode(ASCII) and unicode) by default.But python3 support unicode(UTF-8) by default.


Number is most usable data-type in all languages. The supported data-type inside numbers is
1. int
2. float
3. complex

Int: It contain all type of integer value and the maximum size of this data-type is unlimited. You can assign value as max as possible according to the memory size.

print (a, type(a))

5, <class ‘int’>

It contain decimal values. After decimal it can contain 15 values.

print (a, type(a))

5.004, <class ‘float’>

As mathematics you can define complex value which is also known as imaginary value.

a= 2+7j
print (a,type(a))
(2+7j),<class ‘complex’>


Bytes is a machine understandable language. Human cant read it. So when machine need to communicate with machine then often we use this type of data-type. Networking, socket programming is the best field of programming where this data-type is used frequently. This data-type make system fast because there is not need of changing in encoding unlike another data-type. Such type of variable can store a sequence of bytes ranging from 0 to 255. It is a immutable data-type.

ins = bytes(16)

b’\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00′ <class ‘bytes’>

Non-Primitive data-type:

Collection of primitive type data is known as non-primitive data-type. This type of data can be further separated in primitive data-type. For Example List and Dictionary.

a= [1,2,”Aisangam”] b={“name”:”Aisangam”,”Location”:”India”}


List is a array like data-type in which we can store data in a sequential order. Unlike array it can store heterogeneous data-type element.

li=[‘aisangam’,1,2+5j,8.08] print (li,type(li))

[‘aisangam’, 1, (2+5j), 8.08] <class ‘list’>

Slicing in list:
As we have already discussed in string, List support slicing.



In list you can put any data-type. List is mutable. You can alter list with the help of its index number.

print (li,type(li))

[‘aisangam’, 1, 5, 8.08] <class ‘list’>


It is same like list but have some key differences. It cant be modified as list. Tuple is immutable but list is mutable.
List notation is [] but tuple notation is ().

Some benefits of tuple:
Work as constant
Can be useful to make key for dictionary
Single container etc.


print (tuple,type(tuple))

(‘aisangam’, 1, (2+5j), 8.08) <class ‘tuple’>


Dictionary is a very efficient data structure to store a large data. Dictionary is Un-ordered collection of data in which data is stored in key-pair wise. This is highly optimised for retrieval of particular data from a large data collection.


With the help of key data can be directly access.



In this non-primitive any type of data can be stored as value of any key. At dynamic time you can also change the value as it support mutable behavior.


{‘Company’: ‘AISangam’, ‘name’: ‘ABC’, ‘Key’: 0}

There are some important functions used in dictionary.

print (di.keys())

Output: [‘Company’, ‘name’, ‘Key’]


print (di.values())
Output: [‘AISangam’, ‘ABC’, 0]


print (di.items())
Output: [(‘Company’, ‘AISangam’), (‘name’, ‘ABC’), (‘Key’, 0)]

Add new Key to dict:


{‘Company’: ‘AISangam’, ‘name’: ‘ABC’, ‘Key’: 0, ‘Location’: ‘India’}

Update dict:


{‘TimeZone’: ‘IST’, ‘Company’: ‘AISangam’, ‘name’: ‘ABC’, ‘Key’: 0, ‘Location’: ‘India’}

Delete particlar key from dict:

del di[“Key”] print(di)

{‘TimeZone’: ‘IST’, ‘Company’: ‘AISangam’, ‘name’: ‘ABC’, ‘Location’: ‘India’}


Set is an un-ordered and immutable data structure in python. This data-type can perform mathematical operation like set theory. The common mathematical operation of Set is Union, Intersection, difference etc.

set={“Aisangam”, “India”, “IST”}
print (set)

set([‘India’, ‘Aisangam’, ‘IST’])

In set all the element remain unique. So for removal duplicate entry in list you can take the help of set.

li=[“Aisangam”,”India”,”IST”,”Aisangam”,”IST”] print (li)


set([‘India’, ‘Aisangam’, ‘IST’])

Frozen set:
Frozen set is a traditional type of set. After creating a frozen set it cant be modified. This is immutable data structure.

simple_set = {“Ai”, “Sangam”}
print(“Simple Set”,simple_set)
frozen_set = frozenset([“Ai”, “Sangam”, “India”])
print(“Frozen Set“,frozen_set)
print(“Updated Frozen Set”frozen_set)
frozenset([‘Ai’, ‘Sangam’, ‘India’])

Simple Set,set([‘Ai’, ‘Sangam’, ‘India’])
Frozen Set,frozenset([‘Ai’, ‘Sangam’, ‘India’])
Updated Frozen Set,frozenset([‘Ai’, ‘Sangam’, ‘India’])


In complete tutorial we studied about complete data structure in python.

This Post Has 14 Comments

  1. Aditya Kumar

    First of all it is my pleasure to write to you as I had seen all of your you tube videos as well has read all your blogs. You write effectively with proper documentation and quality. So please keep on doing this work in future also. Such articles are very useful to the common public and especially those who are interested in learning python.

    I have a common question: what is difference between print(‘this is \n a book’) and print(r’this is \n a book’)

    With regards
    Aditya Kumar

    1. AISangam

      Hello Aditya Kumar. It is pleasure to revert back to you. You spoke a lot of things and donot worry we will keep on writing the content. Coming back to your question. Let us see what would be the output when we execute the statement

      print(‘this is \n a book’)

      Output is as below

      this is 
      a book

      As you can see that when the new line comes, the succeeding content will come in the next line.

      Now let us run the next code.

      print(r'this is \n a book')

      Output is as below

      this is \n a book

      So in this the \n is printed as it is. I hope you understand the difference while performing the live example.

      1. Aditya Kumar

        Again a question arise in the mind. This is related to regular expression. I hope asking the question on Regex in the Data Structure is not so appropriate but i hope you would answer me here.

        My question is what is the relation between re.compile and re.match

        1. AISangam

          No problem, you may ask any question anywhere. Please see the explanation from code as below

          prog = re.compile(pattern).............1
          result = prog.match(string)............2
          result = re.match(pattern, string)......3

          In simple words 1+2===>3

  2. Ali

    Hello ai sangam. I want to know how to use argparse.

    If you can provide an example it would be great.

    1. AISangam

      Thanks ali
      Please see the below example where I have concatenated two integers and found the square of them using argparse. Please have a look at the code

      import argparse
      parser = argparse.ArgumentParser()
      parser.add_argument("value1",help="first value:",type=int)
      parser.add_argument("value2",help="second value:",type=int)
      a= parser.parse_args()
      solution1 = a.value1
      solution2 = a.value2

      Please type in the terminal python 1 1
      Output is
      121 as 1 1 are concatenated and becomes 11 and square of 11 is 121

  3. vansh

    Can you show an example where one can use lambda, list and map together.

    1. AISangam

      Thanks for such question.
      Please see the below code

      dict_a = [{'name': 'python', 'type': 'language'}, {'name': 'docker', 'type': 'tool'}]
      print("Printing the dictionary:")
      print("Printing the usecase of lambda+map+list:")
      print(list(map(lambda x:x['name'],dict_a)))

      Output of the code

      Printing the dictionary:
      [{'type': 'language', 'name': 'python'}, {'type': 'tool', 'name': 'docker'}]
      Printing the usecase of lambda+map+list:
      ['python', 'docker']
      1. vansh

        Can you show me the use case of list.

        If so please show it.

        1. AISangam

          This is the simple program which showcase the use of list where vowels in the entered string is counted.

          database = ['a', 'i', 'e', 'o', 'u']
          a = input("Enter the words from which vowels are to be counted:")
          a = a.lower()
          count = 0
          for i in a:
              if i in database:
                  count = count + 1
          print("Please show me count of vowels:", count)
  4. Karam

    I need to know how to convert the dictionary into list. Can you show me the easiest way to do so.

    1. AISangam

      Hello karam. I have provided two of the solutions on Quora. Please read comments for Al Sangam there. Here is the link

      If you like to get simple solution, yes we can implement the same using the list comprehension. Please do see here

      stationary = {'pen': 10, 'pencil': 5, 'paper board': 50}
      keys = [key for key in stationary.keys() ]
      values = [value for value in stationary.values()]
      print("Keys in the dictionary are:",keys)
      print("Values in the dictionary are",values)
  5. Abhi

    Some times, I got struck in pop, remove and delete functions with list in python. Can you help me out,please.

    With regards

Leave a Reply