Learn What a Data Structure Is in Python: Absolute Beginners Tutorial

A data structure is how you organize and store information in a program so you can find it, use it, and change it efficiently. Python gives you four powerful built-in data structures right out of the box: lists, tuples, dictionaries, and sets. This tutorial walks you through each one from scratch, with runnable code and interactive exercises, so you can start using them in your own programs today.

If you have ever stored a value in a variable, you have already worked with data. But variables hold only one piece of information at a time. What happens when you need to track a hundred usernames, a set of unique IP addresses, or a table of configuration settings? That is where data structures come in. They give your data shape, rules, and efficient access patterns so your programs stay fast and organized as they grow.

What Is a Data Structure and Why Does It Matter?

A data structure is a format for organizing, storing, and managing data in computer memory so it can be accessed and used efficiently. Think of it like a filing system. A single sticky note works fine for one phone number, but if you need to manage hundreds of contacts, you need folders, labels, and an organizational system. Data structures are that system for your code.

Every data structure has trade-offs. Some are fast at looking things up but slow at inserting new items. Others are great at preserving order but waste memory on small datasets. Choosing the right one depends on what operations you perform the most in your specific program.

Python comes with four built-in data structures that handle the vast majority of everyday programming tasks: list, tuple, dict, and set. Unlike languages such as Java or C++ where you often import libraries for basic collections, Python includes these as core language features. You can start using them immediately without any imports.

Note

Everything in Python is an object. That includes data structures themselves. When you create a list, you are creating an instance of the list class. This means data structures come with built-in methods you can call, like .append() on a list or .get() on a dictionary.

python
# A single variable holds one value
username = "alice"

# A data structure holds many values in an organized way
usernames = ["alice", "bob", "carol", "dave"]

# You can access any item by its position (index)
print(usernames[0])   # alice
print(usernames[2])   # carol
print(len(usernames)) # 4

In the example above, a plain variable can only hold one username at a time. The list usernames holds four values in a single container, and you can retrieve any one of them by its index position. That is the core value of a data structure: it lets you work with collections of related data as a single unit.

code builder click a token to place it

Build a Python list that stores three fruit names:

your code will appear here...
"cherry" ( fruits ] "banana", [ = ) "apple",

The Four Built-in Data Structures in Python

Python's four built-in data structures each solve a different organizational problem. Understanding what makes each one unique is the first step toward choosing the right tool for any task.

Lists: Ordered and Changeable

A list is an ordered collection of items that you can modify after creation. You create one using square brackets []. Lists allow duplicate values, support mixed data types, and maintain the order in which you add items.

python
# Creating a list
colors = ["red", "green", "blue"]

# Adding an item to the end
colors.append("yellow")
print(colors)  # ['red', 'green', 'blue', 'yellow']

# Changing an item by index
colors[0] = "crimson"
print(colors)  # ['crimson', 'green', 'blue', 'yellow']

# Removing an item
colors.remove("green")
print(colors)  # ['crimson', 'blue', 'yellow']

Tuples: Ordered and Unchangeable

A tuple looks similar to a list but uses parentheses () instead of square brackets. The critical difference is that tuples are immutable. Once you create one, you cannot add, remove, or change its items. This makes tuples ideal for data that should stay constant, like geographic coordinates or RGB color values.

python
# Creating a tuple
coordinates = (29.7604, -95.3698)

# Accessing items by index
print(coordinates[0])  # 29.7604
print(coordinates[1])  # -95.3698

# Tuples support unpacking
latitude, longitude = coordinates
print(latitude)   # 29.7604
print(longitude)  # -95.3698

# This would raise a TypeError:
# coordinates[0] = 30.0
Pro Tip

A single-item tuple requires a trailing comma: singleton = (42,). Without that comma, Python treats the parentheses as a grouping operator, not a tuple constructor. This is a common source of confusion for beginners.

Dictionaries: Key-Value Pairs

A dictionary stores data as key-value pairs inside curly braces {}. Instead of accessing items by a numeric index, you look them up by a descriptive key. Keys must be unique and immutable (strings and numbers are common choices). Since Python 3.7, dictionaries preserve insertion order.

python
# Creating a dictionary
user = {
    "name": "Alice",
    "age": 30,
    "email": "alice@example.com"
}

# Accessing a value by key
print(user["name"])   # Alice

# Adding a new key-value pair
user["role"] = "admin"

# Safely accessing a key that might not exist
print(user.get("phone", "not provided"))  # not provided

# Looping through keys and values
for key, value in user.items():
    print(f"{key}: {value}")

Sets: Unique and Unordered

A set is an unordered collection that automatically discards duplicate values. You create one with curly braces {} or the set() constructor. Sets are excellent for membership testing, removing duplicates from a list, and performing mathematical operations like unions and intersections.

python
# Creating a set — duplicates are automatically removed
tags = {"python", "beginner", "tutorial", "python"}
print(tags)  # {'python', 'beginner', 'tutorial'}

# Adding an item
tags.add("data-structures")

# Checking membership (very fast)
print("python" in tags)     # True
print("javascript" in tags) # False

# Removing duplicates from a list
raw = [1, 2, 2, 3, 3, 3, 4]
unique = list(set(raw))
print(unique)  # [1, 2, 3, 4]
Syntax
["a", "b", "c"]
Ordered
Yes
Mutable
Yes
Duplicates
Allowed
Best for
Ordered sequences you need to modify, such as task lists, logs, or collections of mixed items
Syntax
("a", "b", "c")
Ordered
Yes
Mutable
No
Duplicates
Allowed
Best for
Fixed data that should not change, such as coordinates, RGB values, or database records
Syntax
{"key": "value"}
Ordered
Yes (since Python 3.7)
Mutable
Yes
Duplicates
Keys must be unique; values can repeat
Best for
Labeled data where you look things up by name, such as user profiles, config settings, or API responses
Syntax
{"a", "b", "c"}
Ordered
No
Mutable
Yes
Duplicates
Not allowed
Best for
Unique collections, fast membership checks, and mathematical set operations like union and intersection
spot the bug click the line that contains the bug

This code tries to create a dictionary and print a user's email. One line has a mistake. Find it.

1 user = {
2 ["name"]: "Alice",
3 "age": 30,
4 "email": "alice@example.com"
5 }
6 print(user["email"])
The fix: Change ["name"] to "name" on line 2. Dictionary keys must be immutable types. A list ["name"] is mutable and cannot be used as a dictionary key. Using the string "name" directly is the correct approach. Python would raise a TypeError: unhashable type: 'list' if you tried to run this code.

Mutable vs. Immutable: The Key Distinction

The single most important concept for understanding Python's data structures is mutability. A mutable object can be changed after creation. An immutable object cannot. This distinction affects how you use each data structure, how Python stores it in memory, and whether it can serve as a dictionary key.

Lists, dictionaries, and sets are mutable. You can add items, remove items, or change existing values in place. Tuples and strings are immutable. Any operation that appears to modify them creates an entirely new object instead.

python
# Mutable: lists can be changed in place
colors = ["red", "green", "blue"]
colors[0] = "crimson"     # this works
print(colors)             # ['crimson', 'green', 'blue']

# Immutable: tuples cannot be changed
dimensions = (1920, 1080)
# dimensions[0] = 2560    # TypeError: 'tuple' does not support item assignment

# Why it matters: only immutable types can be dictionary keys
valid_key = (10, 20)      # tuple — works as a key
coordinates = {valid_key: "office"}
print(coordinates[(10, 20)])  # office

# invalid_key = [10, 20]  # list — would raise TypeError as a key
Watch Out

When you assign a mutable object to a new variable, both variables point to the same object in memory. Changing one changes the other. Use .copy() or the list() constructor to create an independent copy if you need separate data.

The distinction between mutable and immutable types shapes how you design functions, pass arguments, and prevent unintended side effects. When a function receives a list as an argument, it receives a reference to the original list. Any changes the function makes to that list are visible to the caller. If you pass a tuple instead, the function cannot modify the original data, which makes your code safer and more predictable.

Python's built-in data structures grouped by mutability. Mutable structures allow in-place changes; immutable ones do not.

How to Choose the Right Data Structure in Python

Picking the right data structure is not about memorizing rules. It is about asking yourself four questions about the data you need to store. Walk through them in order, and the answer usually becomes clear.

  1. Identify whether you need key-based access

    Ask yourself whether each piece of data has a natural label or name. If you need to look up values by a descriptive key like "email" or "city", a dictionary is the right choice. If you only need to access items by their position in a sequence, move to the next step.

  2. Determine whether the data should change

    If you need to add, remove, or update items after creation, use a mutable structure: a list for ordered sequences or a set for unique unordered items. If the data should remain constant throughout your program, use a tuple to prevent accidental modification.

  3. Check whether uniqueness matters

    If every item in your collection must be unique and you do not care about order, use a set. Sets automatically discard duplicates and provide fast membership testing. If you need both uniqueness and key-value pairing, a dictionary enforces unique keys by default.

  4. Confirm ordering requirements

    If the position of each item matters, such as in a ranked list or a sequence of steps, use a list or a tuple. Both preserve insertion order. Dictionaries also preserve insertion order since Python 3.7, but they are accessed by key rather than by numeric index. Sets do not guarantee any particular order.

"Data structures are the fundamental constructs around which you build your programs." — Real Python

Python Learning Summary Points

  1. A data structure is a way to organize and store data in memory so it can be accessed and modified efficiently. Python provides four built-in data structures that cover the majority of everyday programming needs.
  2. Lists are ordered and mutable. Tuples are ordered and immutable. Dictionaries store key-value pairs with unique keys. Sets store unique, unordered elements.
  3. Mutability is the most important distinction. Mutable objects (lists, dicts, sets) can be changed in place. Immutable objects (tuples, strings, frozensets) cannot, and any modification creates a new object.
  4. Choosing the right data structure depends on four factors: whether you need key-based access, whether the data needs to change, whether duplicates are allowed, and whether order matters.
  5. Since everything in Python is an object, each data structure comes with built-in methods. Using these methods (like .append(), .get(), .add(), and .items()) is the idiomatic way to work with data in Python.

Data structures are the foundation of every Python program. Once you understand lists, tuples, dictionaries, and sets, you have the tools to organize anything from a simple collection of names to a complex configuration system. Practice creating each one, experiment with their methods in the interpreter, and pay attention to how mutability affects your code. The more you use these structures, the more natural the choice between them becomes.

check your understanding question 1 of 5

Frequently Asked Questions

A data structure in Python is a way of organizing and storing data so that it can be accessed and modified efficiently. Python has four main built-in data structures: lists, tuples, dictionaries, and sets. Each one handles data differently depending on whether you need ordered access, key-based lookups, uniqueness enforcement, or immutability.

A list is mutable, meaning you can add, remove, or change its elements after creation. A tuple is immutable, meaning once it is created, its contents cannot be changed. Lists use square brackets [] while tuples use parentheses (). Tuples are slightly faster and use less memory than lists because of their immutability.

Use a dictionary when you need to look up values by a descriptive key rather than a numeric position. For example, storing a user profile where you access data by field name (like "email" or "age") is a natural fit for a dictionary. Lists are better when order and position matter, such as a sequence of steps or a ranked collection.

Mutable means the object can be changed after it is created. Lists, dictionaries, and sets are all mutable. You can add items, remove items, or update existing items in place. Immutable objects like tuples and strings cannot be changed after creation, meaning any modification produces a new object.

Sets automatically discard duplicate values when they are created or when items are added. If you pass a list with repeated elements into set(), Python keeps only one copy of each unique value. This makes sets useful for tasks like finding unique items in a collection or checking membership quickly.

Yes. A Python list can hold items of different types, such as integers, strings, floats, and even other lists or dictionaries. This flexibility is one of the reasons lists are the go-to data structure for general-purpose collection handling in Python.

The four main built-in data structures in Python are: list (an ordered, mutable sequence), tuple (an ordered, immutable sequence), dict (a collection of key-value pairs with unique keys), and set (an unordered collection of unique elements). These four structures cover the vast majority of data organization needs in Python programs.

Since Python 3.7, dictionaries maintain insertion order as part of the language specification. This means items come out in the same order you put them in. However, you access dictionary values by key rather than by position, which is the fundamental difference between dictionaries and lists.