7 minutes read
Python Data Structures

Python Data Structures

When people think of programming, they often think in terms of languages and frameworks. Names like Swift, Javascript, Node, Angular, and others often come to mind when people are asked about the tools needed for high-level programming. But just as important as the languages and frameworks are data structures -- fundamental concepts of computer science which are essential to writing code efficiently and creating well-tuned apps and programs no matter your language of choice.

Data structures are methods for organizing, managing, and storing data in a way that allows for easier access and efficient modifications for programmers. Not only do data structures offer methods of storage, but you can also organize data into collections, relate these collections, and perform key operations on the data accordingly. While structures exist across different programming languages, each language has its own specific format and assortment of data structures that beginner programmers need to learn.

In the case of Python, one of the most popular languages worldwide right now, knowing the Python data structures is key to getting the most out of this versatile and dynamic language.

There are two distinct categories of data structures that help power Python:


Built-in data structures

These are data structures that are embedded within the Python language, designed to help make programming easier and allow programmers to find solutions to programming issues quickly.

User-defined data structures

These are data structures that are more broadly applicable (present in a variety of programming languages).

In this guide, we’ll explore both built-in and user-defined data structures to know when using Python, as well as some of the practical applications and impacts on the coding process that these various data structures can provide for you.

Built-in Data Structures in Python 

There are four main categories of built-in data structures when using Python: Lists, Dictionaries, Tuples, and Sets. Here’s what you need to know about each:


A list is a data structure that holds an ordered collection of items. This means you can store any sequence of items in a given list. Lists in Python function the exact same way as any real list (like a shopping list), except that you probably write different items on separate lines in your shopping list, as opposed to Python where you insert commas in between them.

Any list of items should be enclosed in square brackets, which allows Python to understand that you are specifying a list. After you have created a list, you can simply add, remove or search for items in the list whenever you need to find something specific. Since we can add and remove items, a list is known as a mutable data type, which is a fancy way of saying that a list can be altered if needed.


It may be easier to think of a dictionary in Python like an address book. In the case of a Python dictionary, instead of finding someone by looking up their name, Python developers associate keys (the name) with values (details). This requires unique keys (an element that cannot be duplicated with another entry) to function, which allows the list to be sorted by a single variable. This helps keep the integrity of the list and avoid duplicates that would prevent data access.

It should be noted that you can use only immutable objects (like strings) for the keys of a dictionary, but you can use either mutable or immutable objects for the values of the dictionary. This means that the keys themselves should be simple objects for clarity and efficiency.

Pairs of keys and values are identified in a dictionary by employing the notation d = {key1 : value1, key2 : value2}. In terms of formatting, you should note that the key-value pairs are separated by a colon and the pairs are separated themselves by commas and all this is enclosed in a pair of curly braces. Additionally, remember that key-value pairs in a dictionary are not organized in any specific manner. If you need a particular order for your task or project, then you will have to sort through them yourself before using it.


The main purpose of Tuples is to hold together or “sync” multiple objects. You can think of them similarly to lists, but Tuples don’t offer the extensive functionality that a list class can provide. One major and unique feature of tuples is that they are immutable like strings, which means you cannot modify Tuples.

Tuples are defined by specifying items separated by commas within an optional pair of parentheses. They are most usually used in cases where a statement or a user-defined function can accurately assume that the collection of values (i.e. the tuple of values used) will not change.


Sets refer to a collection of unordered elements that are unique, which means that even if the data is repeated more than one time, it would be entered into the set only once. Sets are used when the existence of an object in a collection is more essential than the order itself, or how many times the object occurs. By using sets, you can test an object for membership, figure out whether it is a subset of another set, find the intersection between two sets, and other additional sorting and searching functions.

User-Defined Data Structures in Python 

There are a number of user-defined data structures that you need to use and become comfortable with when programming with Python, including arrays (similar to lists), stacks, queues, trees, linked lists, graphs, and hash maps. Remember, user-defined structures are not unique to Python - these are data structures that are shared by many programming languages, but that help when building Python-based apps as well.


Very similar to lists, the sole difference between arrays and lists is that arrays allow only homogeneous elements to be stored within them, while lists allow for heterogeneous data element storage.


Stacks are a linear type of data structure centered on the principle of Last-In-First-Out (LIFO), which means data which is entered last will in turn be the first to get accessed. Stacks are built using the array structure and include a variety of operations: pushing (adding) elements, popping (deleting) elements and accessing elements only from one point in the stack known as the TOP. This TOP is the indicator of the current position of the stack. Stacks are most often used for applications like recursive programming, reversing words, undoing mechanisms in word editors and others that are similar.


A queue is another form of linear data structure, although queues are based on the principle of First-In-First-Out (FIFO) where the data entered first will be accessed first (so the opposite of a stack). Queues are also built using the array structure, and include operations which can be performed from either end of the queue (so, either head-tail or front-back). Some operations such as adding and deleting elements are called En-Queue and De-Queue, and you can access the elements as well. Queues are most often used as network buffers to help manage and control traffic congestion to an app or site. They are also often employed in running operating systems, and a variety of other functions.


Trees are non-linear data structures which include both roots and nodes. The root is the node from where all the data originates, and the other nodes are the other data points that are available to programmers. The node that precedes is known as the parent and the node that follows is called the child, for obvious reasons. Meanwhile, the final nodes are called the leaves

A tree also includes different levels, which can display the depth of information as someone is viewing or accessing the data. Trees create a hierarchy which can be employed in a variety of real-world applications. For example, HTML pages use trees to differentiate which tag comes under which block.

Linked Lists 

Linked lists are a type of linear data structure, which while not stored consequently are linked with each other by employing pointers. The node of a linked list is composed of data and a pointer called next. Linked lists are most widely used for image viewing applications, music player applications and similar media programs and apps.


Graphs are used to collect and store data in a set of points called vertices (nodes) and edges (edges). Graphs are often used as a data parallel for a real-world map or chart, and can be used to find the various cost-to-distance ratio between the various data points to help find the most efficient or direct path. This makes graphs essential to many applications such as Google Maps, Uber, Grubhub, and other apps that involve some form of geographical distance measurement, to help calculate the most direct route (and thus increase profits for the company).