MITB Banner

Python Data Structures That Every Beginner Should Know About

- List - Queues - Stack - Linked Lists - Circular Linked List - Trees - Graphs - Hash Maps

Share

Python data structures

As of 2020, India recorded as many as 8.2 million Python developers and the number is increasing every passing day. The TIOBE Index for July 2021 further revealed that globally, Python was the third-most popular programming language. Chances of it becoming the #1 programming language was high with Python’s leadership in data mining and artificial intelligence. 

Following the trend, if you are planning to begin learning Python programming language, one must know about data structures. 

What are data structures?  

As the name suggests, data structures hold data in the form of structures or code. In other words, data helps store collections of related data or information. Data structures are mostly used to modify, navigate and access information. They are critical in building real-life applications. To increase the efficiency of the programme, and reduce computational time, one must be aware of which data structures fit their present solutions.

Python has four in-built data structures: 

  • Lists or Array
  • Dictionaries 
  • Tuples 
  • Sets 

We list the eight basic data structures in Python that every beginner must know about: 

List 

These array-like structures allow developers to store data of different types in a sequential manner. For every element in a list, a unique address– called Index, is assigned. 

To create a list, one has to use square brackets and add the element inside of it, accordingly. The elements can be added using the append(), extend(), and insert() functions. An empty list will produce an empty output. 

There are other functions that can be used while working with lists: 

  • len() function returns the length of the list 
  • index() returns the index value of the value passed 
  • count() finds the count of the value passed 
  • sorted() and sort() sort the values of the list 
  • append() to add an item to the end of the list 
  • clear() to clear all items from a list 

Queues 

Linear data structure queue stores data in the first-in-first-out format. That is, unlike lists, a programmer cannot access elements by index. They can only extract the next oldest ement, making it usable for order-sensitive tasks such as online order processing or voicemail storage. 

One can, however, use append() and pop() to implement a queue. Insert and delete operations in queues are called enqueue and dequeue. Queues are used for operations on shared resources such as a printer or CPU core, or to serve as temporary storage for batch systems. 

Stack

Stacks are collections of objects supporting last-in-first-out semantics for inserts and deletes. The linear data structures are built using array structures. However, unlike arrays or lists, stacks do not allow random access to objects.

Adding elements to a stack is called push and removing is called pop. Push operations use the append() method, and pop operations use pop(). 

Stacks are used in language parsing, reversing words, for undo mechanisms in editors and for runtime memory management. 

Linked lists

Sequential collection of data Linked lists use rational pointers on data nodes to link to the following node in that specific list. The first node is called the head node and the last one is called the tail node. 

Contrary to arrays, linked lists do not have objective positions in the list. Instead, they have relational positions that are based on the surrounding nodes. 

Linked lists are used to create advanced data structures such as graphs and trees. They form the building blocks for advanced data structures.

Python does not have in-built implementation of linked lists and requires one to implement a Node class to hold data value. 

Circular linked List 

As the name suggests, when linked list nodes are connected to form a circle, it is termed as circular linked list. In the circular linked list, there is no NULL at the end. Any node can be the starting point, and one needs to stop only when the first node is revisited. 

Circular linked lists are usually used for looping solutions, such as for CPU scheduling, and are used for implementation of advanced data structures such as in Fibonacci Heap. 

Trees

Non-linear data structures Trees have both roots (from where the data originates) and nodes (data points that are made available). Trees are relation-based data structures and represent hierarchical structures. 

All tree roots contain pointers to all elements directly below them. These are called the child nodes. Every child node further has child nodes of its own. However, binary trees can only have two child nodes. Nodes on the same level are termed as sibling nodes. Sibling nodes are connected to child notes using leaf nodes. 

Similar to linked lists, trees are implemented using Node objects. Real world applications of Trees include usage in HTML pages to differentiate between tags. 

Graphs 

These are basically pictorial representations of objects. Graph data structures represent the visual relationship between data vertices or nodes of a graph. Links connected to the vertices are called edges and are used to store data. Usually edges do not indicate the direction of flow between vertices. Directed graphs, like in linked lists, define the direction of relationship. 

Graphs are usually used to convey visual web-structure networks in the form of code. They are used by Google Maps and Uber. Additionally, graphs are used to model social networking sites such as Facebook. 

In Python, graphs are best implemented using dictionaries. 

Hash Maps

Hash Maps are indexed data structures that use a hash function to compute an index with a key into an array of buckets. This key is usually unique and immutable. Hash Maps store huge amounts of data. 

They are similar to dictionaries and are used to implement applications like phonebooks. Hash Maps include the following functions: 

  • set_val(key,value): to insert a key-value pair into the hash map
  • get_val(key): returns the value to which the specified key is mapped 
  • delete_val(key): removes the mapping for that particular key 
Share
Picture of Debolina Biswas

Debolina Biswas

After diving deep into the Indian startup ecosystem, Debolina is now a Technology Journalist. When not writing, she is found reading or playing with paint brushes and palette knives. She can be reached at debolina.biswas@analyticsindiamag.com
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.