Coding Dojo

Coding Dojo Blog logo
Ferris wheel as a Python data structure

Top 3 Python Data Structures Explained

If you’ve just started learning Python, you’ve probably come across Python data structures. Though they might seem complicated at first, data structures are useful tools that will help you solve problems more quickly, carry out tasks more efficiently, and write better code in Python. 

In the following sections, we’ll dive into the different data structures and understand the roles that they play in Python programming. We’ll also cover how you can master data structures to carry out specific projects.  

What Are Data Structures in Python?

Data structures organize and store data in order to make it easier to access, modify, and navigate data. These code structures explain the relationship between data and the different logical operations that you can perform on that data. Data structures also determine the functionalities that you can implement on data and how you collect it. 

Python has four built-in data structures:

  • Lists
  • Tuples
  • Sets 
  • Dictionaries 

You can use these Python data structures to solve specific tasks and situations, as well as provide an easy framework to organize programs. 

Why Are Data Structures Important in Python?

The proper management, organization, and storage of large datasets is important since it allows easier access and modifications. Data structures allow you to organize your data in a systematic and logical way, enabling you to solve problems more quickly and efficiently. 

Some data structures allow you to quickly retrieve specific data from a database, build clear relational or hierarchical connections between data points, or speed up data processing. 

While there are many ways to classify data structures in Python, one way is to categorize them into primitive and non-primitive data types. 

Types of Data Structures in Python

  • Primitive Data Structures – These data structures contain simplified data values and serve as the foundation for manipulating data. The four primitive data structures are integers, float, string, and boolean
  • Non-primitive Data Structures – These data structures store values, as well as a collection of values, in varying formats. The four built-in non-primitive data structures are lists, tuples, dictionaries, and sets. 

For a deeper dive into the core semantics and syntax of Python, check out the official Python Language Reference

Built-in Python Data Structures (Non-primitive)

As the name suggests, these non-primitive data structures are built-in with Python. Most existing Python data structures are modified forms of these non-primitive data structures or have built their foundation on one of these data structures. 

1. Lists in Python

A list is an ordered collection of items and is one of the most essential data structures to implement in any Python project. As these are “ordered collections,” each item in the list has an order which uniquely identifies it. 

You can assign addresses to each element of the list (which are indexes). The index value begins at zero and continues on until the last element (the positive index). There is also the negative index, which starts at -1. This enables you to access elements from the very end to the very beginning. 

When creating a list, you should enclose all the items on the list within square brackets, and separate them by commas. This lets Python know that you’ve created a list.

Here is a basic list: 

List_C = [item 1, item 2, item 3….., item n]  

Lists created in Python are mutable—meaning you can modify them after you’ve created them. This allows a user to search, add, shift, delete, and move elements from a list. 

2. Sets in Python

Sets are an unorganized collection of unique elements. Just like lists, sets are mutable (meaning you can alter, replace, or add them after you create them). And just like lists,

you need to enclose sets within curly brackets to show up in the final output. However, no two values should ever be the same.

You can use sets when the inclusion of an object in a collection is more important than the order of the objects. 

Here is a basic set:

​​set_c = {“item 1”, “item 2”, “item 3”,….., “item n”}

3. Tuples in Python

While tuples are similar to lists, they’re immutable (meaning you can’t alter them after you’ve created them). Like lists, they also present an ordered collection of objects but are purposely designed to have limited functionality. 

While the elements found in a list are enclosed within parentheses, in tuples, the use of parentheses is optional. 

Here is a basic tuple:

tuple_A = (item 1, item 2, item 3,…, item n)

4. Dictionary in Python

In Python, a dictionary is an unorganized and mutable container that stores key-value pairs. You can access the values in the dictionary by entering a unique key. 

You can make dictionaries using curly brackets, but they must include key-value pairs separated with commas. A colon must separate each key from its value. 

Here is the basic syntax:

dictionary = {“key name”: value}

User-Defined Data Structures in Python (Non-primitive)

1. LinkedList in Python

Linkedlists are linear data structures that contain multiple nodes. They’re linked with each other using pointers, while the node of each linkedlist consists of data and a pointer called “next.” These data structures are most often found in music players and image viewing apps.   

LinkedList in Python

Alt Text=”illustration of linkedlist, Python data structure”

Image Source: Alpha Coding Skills

While the standard Python library does not have linkedlists, programmers can implement this data structure by using nodes. 

2. Stack in Python

Stacks are linear data structures that store data in stacks. They’re based on the principles of  First-In/Last-Out (FILO) and Last-In/First-Out (LIFO). The insertion of a new element at one end triggers the removal of an element from the same end. You can use the operations “push” and “pop” to signal insertions and deletions. 

Stack in Python
Stack elements in Python

Alt Text=”illustration of push and pop principle in stack, Python data structure”

Image Source: Alpha Coding Skills

You can implement stacks by using data structures and modules from the Python library. These include queue.LifoQueue, collections.deque, and list.

3. Queue in Python

These linear data structures have items stored in the First-In/First-Out (FIFO) principle. This means that the data entered first will be accessed first. You can build queues using the array structure and have operations that you can perform on both ends of the queue (i.e. head-tail or front-back). 

The operations related to queue include enqueue (adding elements) and dequeue (deleting elements). You can implement queues using data structures and modules from the Python library. These include queue, collections, deque, and list.

4. Tree in Python

These are non-linear data structures and have both roots and nodes. The root is the original node that the data originates from and the nodes are the other available data points. The first node is known as the parent, and the node that follows is known as the child. 

Trees have levels that display the level of depth of the data. In this structure, the bottom nodes are known as leaves. 

Tree in Python

Alt Text=”illustration of the tree, Python data structure”

Image Source: Python Tutorials

5. Graph in Python

In Python, a graph pictorially represents a set of objects, and some object pairs are connected by links. Vertices (nodes) represent the interconnected objects and the links that join the vertices are known as edges. You can use the Python dictionary data type to represent graphs, with the “keys” of the dictionary representing the vertices. The values indicate the edges between the vertices.

6. Hashmap in Python

You can use hashmaps to implement applications (like phonebook) and populate data according to lists, among other applications. A hash function generates the index value or address of the data element. The index value will act as the key to the data value. This allows you to access data more quickly.

Primitive Data Structures in Python

Primitive data structures can only store one type of data. Also, primitive data structures need to contain some type of value and the size depends on the type of data structure. 

1. Integers ‍in Python

In Python, integers are zero, positive, or negative whole numerals. They do not have a fractional part and have unlimited precision. 

Integers can have octal, binary, or hexadecimal values. 

2. Float in Python

In Python, float represents the floating point number. They’re used to represent real numbers and are written with a decimal point that divides the integer and fractional parts.

Python displays float values as 64-bit double-precision values. Meanwhile, the maximum value that any floating-point number can reach is approximately 1.8 x 10308. Python will display any number greater than this by the string inf when rendered in Python. 

3. String ‍in Python

In Python, a string is a collection of alphabets, characters, or words. While it’s one of the most primitive of data structures, it’s also vital to data manipulation. Python has a built-in string class known as str, which is immutable. 

4. Boolean in Python

This is one of Python’s built-in data types, and it represents the truth value of an expression. The Python boolean type has only two possible values: true or false. In other words, you can evaluate any expression and get either true or false in response. 

Learn to Code Python at Coding Dojo

Python is both an exciting and challenging language to learn. Once mastered, you can use Python in a wide variety of applications, ranging from data analysis and visualization, developing software and apps, to running machine learning and artificial intelligence. 

Professionals who are proficient in Python and manipulating Python data structures are also highly sought after in the job market, with the average Python developer earning $115,835 annually!

To take your skills and career to the next level, consider checking out our software development courses on codingdojo.com. We also have a free virtual event—Intro to Python—where we’ll introduce you to the fundamentals of Python. We hope to see you soon!