A light on Data Structures in Python

Muhammad Zubair
4 min readAug 30, 2021

--

Python is the most popular and widely used programming language in the world. It has the easiest syntax when compared with all the other programming languages in the globe.

Let us see python in action by putting some light on some of the most popular Data Structures used in every day programming. But before that, do you know what a Data Structure actually is? 🤔

What is a Data Structure?

A data structure is a particular way of organizing data in a computer so that it can be used effectively. For example, we can store data in following different ways depending upon different use cases:

  1. An array like [1,2,3,4,5] which can be used for one-dimensional data traversals and modifications.
  2. A 2d array similar to above but now expanding along both the axis. It can be used for storing matrices and other board like data.
  3. A stack and a queue with special properties like from where to add or remove the elements (from starting or from the end of array?)

And much more……….

So let us deep dive into some famous and mostly used data structures and see how we can use super powers of Python in them?😎

We will cover following data structures in this article:

  1. Trees
  2. Stack
  3. Graphs

So, lets get started: 🙂

  1. Trees:

A tree is a nonlinear hierarchical data structure that consists of nodes connected by edges. It helps in fast data retrieval. Look at the picture of a typical tree below:

Tree data structure
A sample Tree data structure

Let us see how can python implement a tree:

python code for implementing tree
  • We have used python class to define a reusable tree data structure.
  • We defined two class methods:

1) __init__ (for initializing the tree at the time of creation)

2) __str__ (for printing a tree in a readable format)

  • A tree is defined by one or more nodes. A single node itself is also a tree.
  • A node has info(which is its own value), left(node value at its left) and right(node value at its right).
  • At line11, we initialize a tree by using Tree class. This tree contains ‘1’ at the root which is connected to two other subtrees having root at ‘2’ and ‘3’ and so on…………
  • Finally, we print it. (recall that print function invokes the class function __str__ for printing in a readable format)

2. Stack:

A stack is a linear data structure that follows the principle of Last In First Out (LIFO). This means the last element inserted inside the stack is removed first. It can be used when we want to operate from one end of the array only.

Look at working of a typical stack below:

Working demo of Stack data structure

Let us see how can python implement a Stack:

As you can see above, this is the real power of python. Only 2 functions append() and pop() are enough to deal with a stack. 😊

3. Graph:

A Graph is a non-linear data structure consisting of nodes and edges. It is a collection of nodes that have data and are connected to other nodes. Edge is a line that connects two nodes.

Lets look at a typical graph below:

Let us now implement it with python.

Here we have defined a graph through using special power of python; the dictionary. It shows a node and a list of the nodes that are connected with that one. So, we can define a function that takes this Graph Dictionary as an input and convert it to return the edges of this graph.

Oh yeah! THIS LOOKS AWESOME. We give graph information as an input and we get edges as an output. We do this literally by using a list named “edges” in above code and then insert tuples in it containing endpoints of every possible edge in the graph. So, we get edges as a result.

That is very much it for this topic and for this blog. And remember this is just a simple and basic Data Structure stuff that you can do with python. We implemented it in such a few lines of code compared to other Programming languages like C++,C, Java. And that is the real power of this language.

Thanks for reading.

--

--