Data Structures in Python

Data Structures in Python | List, Tuple, Dict, Sets, Stack, Queue ...

Data Structures In Python

Hello Guys, Today Iam going to tell you about Data Structures in Python.After this, you all are able to create projects with any logical problems.this article make your skill strong and help you all to understand the basics concepts of the Data Structure in Python.

But again, In the all my past articles, I told you first you have to understand the basic concepts of the Python.

Python is very famous language and mostly used worldwide on the  different fields such as making ,websites,artificial intelligence and much more.

 For websites we use Flask,Django Frameworks.

For help and want to learn the basics of the Python :-

But to make biggest projects we all have tounderstand data structure, data plays a very important role which means that this data should be stored efficiently and the access to it must be timely. So how do you achieve this? We use something called Data Structures

With that being said, let us go through the topics we will learn about Data Structures in Python.

This article has been shown into the following parts, let’s see it first:
     Arrays vs. List
     Linked Lists

Now let’s start our tutorial on Data Structures in Python  :)

What is a Data Structure?

Fundamental Data Structures in Python –

Data Structure is the Organizing, managing and storing data is important to create programs in python and  it enables easier access and efficient modifications. Data Structures allows you to organize your data in such a way that enables you to store collections of data, relate them and perform operations on them accordingly.

Types of Data Structures in Python

Python has implicitly support for Data Structures which enable you to store, access and modify  data. These structures are called List, Dictionary, Tuple and Set.
Python allows its users to create their own Data Structures enabling them to have full control over their functionalities of the programs. The mostly used Data Structures are Stack, Queue, Tree, Linked List and so on, which are also available to you in other programming languages.
So now we have to understand and apply these on our Python Programs.The main problem is how we implement our Data Structure knowledge in our projects.
There are two types of Data Structure in Python :-

·         Built-in Data Structures
·         User-defined Data Structure

let’s learn it :-

Built-in Data Structures

Algorithms and Data Structures in Python | Udemy


Lists are used to store data of different data types in a sequential manner. There are addresses assigned to every element of the list, which is called as Index. The index value starts from 0 and goes on until the last element called the positive index. There is also negative indexing which starts from -1 enabling you to access elements from the last to first.

Let us now understand about lists it’s better to understand with the help of python programs.

Creating a list

To create a list, you use the square brackets and add elements into the program of the python. If you do not pass any elements inside the square brackets, you get an empty list as the output.

list =[]
list2 =[1, 2, 3, 'ram', 5.055]


[1, 2, 3, ‘ram’, 5.055]

Add Elements in the list

Adding the elements in the list can be achieved with using the append(), extend() and insert() functions in the programs.

·      The append() function adds all the elements passed to it as a single element.
·      The extend() function adds the elements one-by-one into the list.
·      The insert() function adds the element passed to the index value and increase the size of the list too.

list =[1, 22, 32]

list.append([234, 25])

list.extend([234, 'hello world'])

list.insert(1, 'new one')


[1, 22, 32]
[1, 22, 32, [234, 25]]
[1, 22, 32, [234, 25], 234, ‘hello world’]
[1, ‘new one’, 22, 32, [234, 25], ‘hello world’]

Deleting Elements

·      To delete elements, use the del keyword which is built-in into Python but this does not return anything.
·      If you want the element back, you use the pop() function which takes the index value.
·      To remove an element by its value, you use the remove() function.

list =[21,23,45,65,’new ram’,21]
list.remove('new ram')
value =list.pop(1)
print('Popped Element: ', value, ' List remaining: ', list)


[21,23,45,65,’new ram’,21]
Popped Element: 23 List remaining:
[21,45,65,’new ram’,21]

Accessing Elements

Accessing elements is the same as accessing Strings in Python. You pass the index values and hence can obtain the values as needed.
list =[21,23,45,65,’new ram’,21]

fori inlist:


new ram
[21,23,45,65,’new ram’,21]
new ram
[21, 23]
[21,’new ram’,65,45,23,21]


Dictionaries are used to store key-value pairs. To understand this better, think of a phone directory where hundreds and thousands of names and their corresponding numbers have been added.

Now the constant values here are Name and the Phone Numbers which are called as the keys  And the various names and phone numbers are the values that have been fed to the keys. If you access the values of the keys, you will obtain all the names and phone numbers.

So that is what a key-value pair is. And in Python, this structure is stored using Dictionaries.

Creating a Dictionary

Dictionaries can be created using the braces or using the dict() function. You need to add the key-value pairs whenever you work with dictionaries in the python.

dict ={}
dict ={1: 'Python', 2: 'Java'}

{1: ‘Python’, 2: ‘Java’}

Changing and Adding key, value pairs

To change the values of the dictionary, you need to using the keys. So, you firstly access the key and then change the value.

To add values, you simply just add another key-value pair example is given below :-

dict ={'one': 'Python', 'two': 'Java'}
dict['two'] ='C++'
dict['three'] ='PHP'

{'one': 'Python', 'two': 'Java'}
{'one': 'Python', 'two': 'C++'}
{'one': 'Python', 'two': 'C++', ‘three’: ‘PHP’}

Deleting key, value pairs

·      To delete the values, you use the pop() function which returns the value that has been deleted.
·      To retrieve the key-value pair, you use the popitem() function which returns a tuple of the key and value.
·      To clear the entire dictionary, you use the clear() function.

dict ={'one': 'Python', 'two': 'Java', 'three': 'PHP'}
value =dict.pop('three')
print('Value:', value)
print('Dictionary:', dict)
value1 =dict.popitem()
print('Key, value pair:', value1)
print('Dictionary', dict)
print('n', dict)

Value: PHP
{'one': 'Python', 'two': 'Java'}
Key, value pair: (‘two’, ‘Java’)
Dictionary {‘one’: ‘Python’}

Accessing Elements

You can access elements using the keys only. You can use either the get() function or just pass the key values and you will be retrieving the values.

dict ={'one': 'Python', 'two': 'Java'}



Tuples are the same as lists are with a problem that the data once entered into the tuple cannot be changed . You can change it only program is when the data inside the tuple is mutable, only then the tuple data can be changed.

Creating a Tuple

You create a tuple using parenthesis or using the tuple() function.

tuple =(1, 2, 3)

(1, 2, 3)

Accessing Elements

Accessing elements is the same as it is for accessing values in lists.

tuple =(1, 2, 3, 'ram one')
fori intuple:

ram one
(1, 2, 3, ‘ram one’)
(1, 2, 3, ‘ram one’)

Appending Elements

To append the values, you use the ‘+’ operator which will take another tuple to be appended to it.

tuple =(1, 2, 3)
tuple += (4, 5, 6)

(1, 2, 3, 4, 5, 6)


Sets are a collection of unordered elements that are unique. If you repeated the data more than one time, it would be entered into the set only once mean’s repeatation’s not effect it.

It resembles the sets that you have learn in arithmetic. The operations also are the same as the with the arithmetic sets.

Creating a set

Sets are created using the following braces but instead of adding key-value pairs, you just pass values to it.

set ={1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5,6,7}

Adding elements

To add elements, you use the add() function and pass the value to it.

set ={1, 2, 3}

{1, 2, 3, 95}

Operations in sets

The different operations on set such as union, intersection and so on are given below:-

·      The union() function combines the data on both the sets.
·      The intersection() function finds the data on both the sets.
·      The difference() function deletes the data on both sides and outputs data present only in the set passed.
·      The symmetric_difference() does the same as the difference() function but outputs the data which is remaining in both sets.

set ={1, 2, 3, 4}
set2 ={5, 6, 7, 8, 1, 2}
print(set.union(set2), '----------', set | set2)
print(set.intersection(set2), '----------', set & set2)
print(set.difference(set2), '----------', set -set2)
print(set.symmetric_difference(set2), '----------', set ^ set2)

{1, 2, 3, 4, 5, 6} ———- {1, 2, 3, 4, 5, 6}
{3, 4} ———- {3, 4}
{1, 2} ———- {1, 2}
{1, 2, 5, 6} ———- {1, 2, 5, 6}

Now that you all have understood the built-in Data Structures, let’s start to understand the user-defined Data Structures. User-defined Data Structures, the name itself telling that users define how the Data Structure would work and define functions in it. This gives the user whole control over how the data needs to be saved, manipulated.

A user have the power to change, access the data structure according to the user.

User-Defined Data Structures

Python Data structure

Arrays vs. Lists

Arrays and lists are the same structure with one difference. Lists allow heterogeneous data element storage whereas Arrays allow only homogenous elements to be stored within them.


Stacks are linear Data Structures which are based on the principle of Last-In-First-Out (LIFO) where data which is entered last will be the first to get accessed. It is built using the array structure and has operations namely, pushing (adding) elements, popping (deleting) elements and accessing elements only from one point in the stack called as the TOP.

This TOP is the pointer to the current position of the stack. Stacks are prominently used in applications such as Recursive Programming, reversing words, undo mechanisms in word editors.


Data Structures in Python: An Interview Refresher - Learn ...

A queue is also a linear data structure which is based on the principle of First-In-First-Out (FIFO) where the data entered first will be accessed first. It is built using the array structure and has operations which can be performed from both ends of the Queue, that is, head-tail or front-back. 

Operations such as adding and deleting elements are called En-Queue and De-Queue and accessing the elements can be performed.

Queues are used as Network Buffers for traffic congestion management, used in Operating Systems for Job Scheduling.


Trees are non-linear Data Structures which have root and nodes. The root is the node from where the data originates and the nodes are the other data points that are available to us.

The node that precedes is the parent and the node after is called the child. There are levels a tree has to show the depth of flow information. The last nodes are called the leaves. Trees create a hierarchy which can be used in biggest and  lot of real-world applications such as the HTML pages use trees to distinguish which tag comes under which block. It is also efficient in searching purposes.

Linked List

Linked lists are linear Data Structures which are not stored consequently but are linked with each other using pointers. The node of a linked list is composed of data and a pointer called next. These structures are most widely used in those applications those who want a link list in those projects and many more projects.


Graphs are used to store data collection of points called vertices (nodes) and edges (edges). Graphs can be called as the most accurate representation of a real-world map. They are used to find the various cost-to-distance between the various data points called as the nodes and hence find the least path. Many applications such as Google Maps, Uber, and many more use Graphs to find the least distance and increase profits in the best ways.


HashMaps are the same as what dictionaries are in Python. They can be used to implement applications such as phonebooks, populate data according to the lists.

Here we give  a example to you all to better understand the hashmaps topic :-

Phonebook = {
       ‘first number’: ‘9891266435’,
      ‘second number’: ‘989125475’,
      ‘third number’: ‘9891266446’,
For n,v in phonebook.items():
print(n + ‘ _ ’ + v)

first number: 9891266435

second number: 989125475
third number: 9891266446

That wraps up all the prominent Data Structures in Python. I hope you have understood by this articlebuilt-in as well as the user-defined Data Structures that we have in Python and why they are important.


Post a Comment