When writing code in Python, data structures can provide different ways to store data. Much like with other programming languages, data structures in Python are useful in different situations.
Here, we'll be looking at what data structures are, the different kinds available to you in Python, and what the pros and cons of each are.
What Are Data Structures?
Before we look at data structures specifically in the context of Python, we need to understand what they are in a general sense.
Data structures are a key part of computer science when it comes to organizing, managing, and storing data, and in almost every career with Python, you'll need to know how they work.
There are a few different kinds and their uses will affect how quickly a program runs and how efficiently memory is allocated for example.
Programmers need to think carefully about data structures, whether they're writing code in Python or another popular programming language like JavaScript, Java, C#, C++, etc.
Common data structures include arrays, lists, sets, dictionaries, queues, stacks, trees, and graphs, with some of the terminology varying according to the programming language you're writing in.
Why Do We Need Data Structures?
Data structures are important because they can allow programmers to manipulate and use the data in more complex ways.

One of the key reasons we use data structures in computer science is so that we can organize data efficiently.
Imagine you were in a supermarket and there wasn't any rhyme or reason to how the products were organized. It helps that similar products are alongside one another so that you can compare them when purchasing without having to go from one end of the store to the other.
Data structures can make operations quicker, too. Like with our supermarket example. It'd be much quicker if related products were next to each other, too.
Data structures can also optimize memory. Certain types of data structures are more memory-efficient than others and when you use very large datasets, this can become a hugely important part of your programming.
This isn't all for the code itself, though. Choosing the right data structure can help the programmer or programmers because it can make code much easier to read and maintain.
Since the pros and cons of each data structure depend on the situation, understanding and using the appropriate data structure is a key skill that programmers need to learn when learning how to code in Python.
What Are the Different Kinds of Data Structures in Python?
As the Python programming language is our focus in this series of articles, let's see some of the different kinds of data structures you can use and encounter when coding in Python.

Lists
Lists are built-in data structures in Python that contain ordered elements. They're one of the most commonly used data structure types.
Lists are “mutable”, which means you can alter them by adding and removing values within the list. The elements within a list are found according to their position in the list, which is done through indexing.
Arrays
Bytearrays
Dictionaries
Lists
Sets
Booleans
Bytes
Floating-point numbers
Frozensets
Integers
Strings
Tuples
With lists in Python, you don't need to specify their size, which is why elements can be added and removed without causing problems.
Lists don't even need to include data of the same type. Within lists in Python, you can include integers, floats, strings, and even more lists.
The Pros and Cons of Lists in Python
Pros | Cons | ||
---|---|---|---|
Dynamic size | This can make lists a versatile way to store data. | Search times | Due to the way lists are indexed, search times can be much longer with bigger datasets |
Mutable | Mutability allows you to edit, add, or remove elements in a dataset. | Inefficient insertion and deletion of elements | In certain cases, you may need to shift the elements within your list when adding, removing, or modifying elements. |
Python operations | There are several built-in operations in Python you can use to manipulate data in lists |
Tuples
Tuples, which can be pronounced “too-ple” or “tuh-ple”, are another kind of built-in data structure in Python.
In a similar way to lists, they also provide an ordered dataset but unlike lists, tuples are “immutable”, which means they cannot be changed or altered. Like lists, tuples can include different types of data.
Generally, you'd choose a tuple for your data structure when you want to ensure that the order of the elements within the data structure is maintained and that nothing is altered.
The Pros and Cons of Tuples in Python
Pros | Cons | ||
---|---|---|---|
Immutable | Immutability offers security | Fewer options | The immutable nature of tuples means that programmers have fewer options for manipulating them. |
Sorting | Tuples can be sorted and automatically maintain their order. | Immutable | Immutability means that elements can't be added, removed, or modified and can be a con in certain circumstances. |
Fixed size | As tuples have a fixed size, they're more memory efficient. |
Sets
While lists and tuples are ordered data structures, sets are not. In a set, there is no particular order to the elements within the set, which means they're not indexed and cannot be found or accessed according to their order.
Within a set, you can only have unique elements. You cannot add duplicate elements to a set, but you can add and remove elements, which means that sets are mutable.
The Pros and Cons of Sets in Python
Pros | Cons | ||
---|---|---|---|
Mutable | With mutability, you have the option to add, remove, or modify elements | Unordered data | Much like with indexing, you might sometimes need your data to be ordered. |
Operations | Sets can be efficiently manipulated using multiple operations | No duplicates | Certain datasets will require duplicates, rendering sets unusable in certain instances. |
Dictionaries
Dictionaries in Python are a data structure that uses unordered elements. However, dictionaries store data using key-value pairs, which are two data elements that are linked. From the name, you can probably guess that these are a key and a value.
Keys work in a similar way to indexing. You can have duplicate values, but the key for each value has to be unique. They're useful as you can make the keys something that the programmer understands like "name", "age", "address", etc.
This allows you to organize the data within the dataset by using unique keys. You can update existing values by adding a key that already exists. Since you can add, remove, and modify the elements in dictionaries, they are mutable.
The Pros and Cons of Dictionaries in Python
Pros | Cons | ||
---|---|---|---|
Conflict-proof | The nature of the unique key-value pairs mean that you can't have duplicates that cause conflicts. | High memory overhead | As you need unique keys, the Python interpreter will consume more memory. |
Mutable | Elements can be added, removed, and modified | Immutable keys | Keys cannot be modified, added, or removed. |
Dynamic | The size of your dictionaries can change as you add and remove elements |
You might be interested in learning about functions in Python, too.
Find Out More About Data Structures in Python
Data structures and their pros and cons can be quite a lot to take in, especially if you're new to computer science or programming.
For many people, learning by doing is probably the best way to get to grips with Python and data structures, but it always helps to have somebody who can help you.

Teaching Yourself Python
There are plenty of ways to teach yourself Python and even more reasons why you should! You can find books, online resources, and even the Python community to help you with projects and activities.
Self-guided study is also an option. Platforms like Coursera, edX, Udemy, and Codecademy all offer different courses.
Studying Python in a Class or on a Course
Taking a class in Python with an actual teacher or professor is also an option. Students in college are in the best position when it comes to picking a computer programming, computer science, or Python class as part of their studies.
Learn Python with a Private Tutor
Last but not least, you can study Python and data structures with a private tutor. Unlike online learning platforms and college classes, tutoring provides you with more options over what you learn and how you choose to learn it.

On the Superprof website, you can search for computer programming tutors or Python tutors and even if there aren't any in your local area, there are thousands around the world who can teach you online.
With most offering the first lesson for free, you can try a few out before choosing the one that's right for you!