What Are Python Data Structures?

Python data structures are essentially containers for different kinds of data. The four main types are lists, sets, tuples and dictionaries.

Written by Anthony Corbo
Published on Jan. 03, 2023
Python data structures image of a tablet with a watermark of abstract data and computer hardware overlaid
Image: Shutterstock / Built In
Brand Studio Logo
REVIEWED BY
Peter Grant | Jan 09, 2023

Python data structures organize and group data according to type. There are four main types of Python data structures.

Python Data Structures: What Are the 4 Main Types?

  1. Lists
  2. Sets
  3. Tuples
  4. Dictionaries

Related Reading From Built In ExpertsPython Tuples vs. Lists: When to Use Tuples Instead of Lists

 

What Are the 4 Built-In Python Data Structures?

The four primary data structures utilized in Python are lists, sets, tuples and dictionaries.

 

Lists

Lists are a type of data structure containing an ordered collection of items. They are crucial to executing projects in Python. 

Every item contained within a list has an inherent order used to identify them, which remains consistent throughout the life of the list. Lists are mutable, allowing elements to be searched, added, moved and deleted after creation. Lists can also be nested, allowing them to contain any object, including other lists and sublists.

More on Python ListsHow to Append Lists in Python

 

Tuples

A tuple contains much of the same functionality as a list, albeit with limited functionality. The primary difference between the two is that a list is immutable, meaning it cannot be modified or deleted. Tuples are best when a user intends to keep an object intact throughout its lifetime to prevent the modification or addition of data.

 

Sets

A set is a collection of unique elements with no defined order, which are utilized when an object only needs to exist within a collection of objects and its order or number of appearances are not important.

 

Dictionaries

Dictionaries are unique and immutable objects that consist of key value pairs and are accessible through unique keys in the dictionary.

Find out who's hiring.
See all Data + Analytics jobs at top tech companies & startups
View Jobs

 

What Are User-Defined Data Structures in Python?

User-defined data structures add additional functionality to Python, thereby allowing users to access, modify or preserve data in specific ways. In addition to Python’s built-in data structures, there are a number of user-defined data structures you can use, such as arrays, stacks, queues, trees and more.

Python Data Structures — Lists, Tuples, Sets & Dictionaries Tutorial. | Video: Joe James
  • Arrays: Arrays function similarly to lists, but allow only homogeneous elements to be stored within them, whereas lists may contain heterogeneous elements.
  • Stacks: Stacks are linear data structures in which the data that is published last may be accessed first. Stacks are commonly utilized in recursive programming and for undo functions in word processors.
  • Queues: A queue works oppositely of a stack and is based on the First-In-First-Out principle, that is the data entered first may be accessed last.
  • Trees: Trees are non-linear data structures that incorporate roots and nodes to create a hierarchy of data. These structures are used heavily on HTML pages.

More From Built In Python Experts3 Ways to Write Pythonic Conditional Statements

 

Python Data Structures: Advantages and Disadvantages

Each data structure offers a different way of completing tasks such as sorting, inserting and finding, but efficiency depends on the situation.

No data structure is ultimately better than another, but using one for a task it is not designed to support may lead to longer workflows, or worse, skewed data

  • Linked lists are purpose-built for inserting and deleting data but only offer sequential access to this data, which means searching and sorting will be problematic.
  • Similar to a list, tuples can be used for outputting either an entire tuple or individual elements, but they use less memory space and do not allow sorting, adding, replacing or deleting elements.
  • Sets are intentionally designed to be limited but excel in checking for a value’s existence and avoiding duplicates in a set.
  • Dictionaries allow data to be collected in key-value pairs, making them excellent for quick retrievals in unstructured documents, but are too limited to work with large amounts of tabular data.
  • Arrays are easy to create and excellent for completing tasks that involve working with sequential data but searching, sorting, inserting and deleting will pose items once you shift items. 

Stacks are excellent for adding or removing data that was last entered within the database, and queues are meant to add or remove data first entered in the data set, but if you want to pull an item from the middle of the set, you’ll benefit from using a different data structure. 

Binary search trees allow you to access, sort and delete data quickly while maintaining the sorted order of elements once retrieved. Despite these advantages, binary search trees require a tedious amount of work to create and manage.

Explore Job Matches.