data structure

In programming, a data structure is a way of organizing and storing data so that you can access and modify it efficiently. Data structures are a fundamental aspect of programming, allowing you to manage data in a way that optimizes performance and resource usage.

Python provides several built-in data structures, such as lists, tuples, sets, and dictionaries, each serving different purposes and offering various methods for data manipulation.

Choosing the right data structure is crucial for optimizing your code and ensuring that it runs efficiently.

Examples

Here are some quick examples illustrating the use of some common data structures in Python:

Python
>>> # List
>>> fruits = ["apple", "banana", "cherry"]
>>> fruits.append("mango")
>>> fruits
['apple', 'banana', 'cherry', 'mango']

>>> # Dictionary
>>> person = {"name": "Alice", "age": 30}
>>> person["age"] = 31
>>> person
{'name': 'Alice', 'age': 31}

>>> # Set
>>> unique_numbers = {1, 2, 2, 2, 2, 3}
>>> unique_numbers
{1, 2, 3}
>>> unique_numbers.add(4)
>>> unique_numbers
{1, 2, 3, 4}

>>> # Tuple
>>> point = (10.0, 20.0)
>>> point
(10.0, 20.0)

Tutorial

Common Python Data Structures (Guide)

In this tutorial, you'll learn about Python's data structures. You'll look at several implementations of abstract data types and learn which implementations are best for your specific use cases.

basics data-structures python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated March 6, 2025 • Reviewed by Martin Breuss