Python Data Structures That Every Beginner Should Know About

- List - Queues - Stack - Linked Lists - Circular Linked List - Trees - Graphs - Hash Maps
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.

AIM Daily XO

Join our editors every weekday evening as they steer you through the most significant news of the day, introduce you to fresh perspectives, and provide unexpected moments of joy
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.

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: 


Download our Mobile App



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 

Sign up for The Deep Learning Podcast

by Vijayalakshmi Anandan

The Deep Learning Curve is a technology-based podcast hosted by Vijayalakshmi Anandan - Video Presenter and Podcaster at Analytics India Magazine. This podcast is the narrator's journey of curiosity and discovery in the world of technology.

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

Our Upcoming Events

24th Mar, 2023 | Webinar
Women-in-Tech: Are you ready for the Techade

27-28th Apr, 2023 I Bangalore
Data Engineering Summit (DES) 2023

23 Jun, 2023 | Bangalore
MachineCon India 2023 [AI100 Awards]

21 Jul, 2023 | New York
MachineCon USA 2023 [AI100 Awards]

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox
MOST POPULAR

Council Post: From Promise to Peril: The Pros and Cons of Generative AI

Most people associate ‘Generative AI’ with some type of end-of-the-world scenario. In actuality, generative AI exists to facilitate your work rather than to replace it. Its applications are showing up more frequently in daily life. There is probably a method to incorporate generative AI into your work, regardless of whether you operate as a marketer, programmer, designer, or business owner.

Meet the Tech Fanatic, Deedy

Debarghya Das or Deedy is the founding engineer of internal enterprise search space Glean, a company that strives to solve workplace search queries