Data structures in python, Fundamentals

DATA STRUCTURES IN PYTHON

  • October 26, 2022

How to implement data structures in python?

Data Structures allow 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. It is easier to get the data and modify the data. The data structure allows us to store the data, modify the data, and also allows comparing the data to others. Python divided the data structures into two types.

  • Build-in data structures.
  • User- Defined data structures.
Features Images

BUILD-IN DATA STRUCTURES:

  • List
  • Tuple
  • Dictionary
  • Set

List:

  • A list is a collection of elements with duplicates. A list is a non-homogeneous data structure that stores the elements in a single row and multiple rows and columns.
  • The list can use nested among all, it can be represented by [ ]. The list is mutable i.e we can make any changes to the list.
  • Creating a list is as simple as putting different comma-separated values between square brackets.

Tuple:

  • A tuple is an immutable collection of elements. i.e once the elements are entered into the tuples cannot be changed.
  • Tuples cannot be modified, added, or deleted once they’ve been created. Lists are defined by using parentheses to enclose the elements, which are separated by commas. Unlike lists, tuples come with limited functionality.
  • The tuple is immutable i.e we cannot make any changes in the tuple.
  • For example, A=”(5, 9, 11, 3, 22, 14)” is a “6-tuple.”

Dictionary:

A dictionary refers to a hashed structure of various pairs of keys and values. We can access the elements present in a dictionary using the key values. Dictionary is mutable, but Keys must be of immutable type (string, number, or tuple with immutable elements) and must be unique.

For example: Dict = {'Tom': 18,'Jerry':12}

print (Dict['Tiffany'])

Dictionary is listed in curly brackets, inside these curly brackets, keys and values are declared. Each key is separated from its value by a colon (:), while commas separate each element.

Set:

  • Sets are collections of unordered unique elements, The set data structure but stores in a single row. The set will not allow duplicate elements.
  • It is mutable i.e we can make any changes in the set. The set operation is equal to arithmetic sets.
  • Sets are written with curly brackets ({}), being the elements separated by commas.

USER-DEFINED DATA STRUCTURES

User-defined data structures are not predefined or built-in functions this kind of data structure is achieved using writing based on their algorithm logic. There are many data structures that can be implemented this way. Here the following are the user-defined data structures.

  • Stack
  • Queue
  • Tree
  • Linked List
  • Graph
  • Hash Maps

Stack:

Stacks are linear Data Structures that are based on the principle of Last-In-First-Out where data that is entered last will be the first to get accessed. It is built using the array structure and has operations namely; pushing (adding) elements, and popping (deleting) elements and they have a pointer called TOP which is to track the top of the element.

There are two types of operations in Stack

  • Push – In this operation where data is added to the stack.
  • Pop – In this operation where the data is deleted from the stack.

Stacks are simple data structures that allow us to store and retrieve data sequentially.

Features Images

Queue:

Queue follows the First In First Out (FIFO) rule – the item that goes in first is the item that comes out first. We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end. For example, people waiting in line for a rail ticket, or movie tickets form a queue.

Basic Operation of Queue:

  • Enqueue: Add an element to the end of the queue
  • Dequeue: Remove an element from the front of the queue
  • IsEmpty: Check if the queue is empty
  • IsFull: Check if the queue is full
  • Peek: Get the value of the front of the queue without removing it
Features Images

Tree:

  • A tree will be the type of non-linear data structure. It stored the elements in a tree structure like the parent and child node process. It also follows the root and nodes.
  • Availability of the various types of data structures and the algorithms associated with them, task performance has become easy and efficient way. A tree having levels indicates the depth of the information. The last node is also called the leaves.
  • Trees create a hierarchy that can be used in a lot of real-world applications such as HTML pages use trees to distinguish which tag comes under which block.
Features Images

Linked list:

It consists of a sequence of elements that are connected to each other. Python does not have linked lists in its standard library. Each element of a linked list is called a node, and every node has two different fields:

  1. Data contains the data field’s holds data and the next field points to the next node.
  2. Next contains a reference to the next node on the list.
Features Images

Graph:

  • Graphs are closely representing real-world geographical locations. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph.
  • They are used to find the various cost-to-distances between the various data points called nodes and hence find the least path.
  • It is mostly used in map-related applications like google maps, Rapido, and Uber for finding the shortest distance for better performance.
Features Images

Hash maps:

  • Hash map is a type of data structure that maps keys to it used to store the data in key-value pairs like a dictionary in python.
  • They can be used to implement applications such as phonebooks, populate data according to lists, and much more. We should use it when searching for items based on a key and quick access time is an important requirement.
  • We should avoid using Hash Map when it is important to maintain the same order of items in a collection.
Features Images

Conclusion

I hope you have understood the built-in as well as user-defined Data structures that we have in Python and why they are important.

Author Images
Author:John Gabriel TJ

Managing Director || Sr. Data Science Trainer || Consultant || Made 150+ Career Transitions || Helping people to Make Career Transition with a Customized RoadMap based on their past experience into Data Science

Follow me :