Python gives you four built-in data structures right out of the box: list, tuple, dict, and set. Understanding what each one does — and when to reach for it — is one of the first real skills you build as a Python programmer.
When you write a program, you almost always need to group data together. A shopping cart holds multiple items. A user profile holds a name, an age, and an email address. A guest list holds names you want to check against. Python's built-in data structures give you a way to handle all of these cases — and the right choice depends on what you need to do with the data.
What Is a Data Structure?
A data structure is a way of organising and storing data so that you can work with it efficiently. In Python, the four built-in data structures are containers — objects that hold other objects. Each container has its own rules about order, duplicates, and whether the contents can change after creation.
The word "built-in" means these structures are part of Python itself — you do not need to import any library to use them. They are available in every Python script from the moment you start writing.
Think of data structures as different types of storage. A list is like a numbered shelf where you can add, remove, and rearrange items. A tuple is like a sealed package — the contents are fixed once it leaves the warehouse. A dictionary is like a filing cabinet with labelled folders. A set is like a bag that rejects duplicates automatically.
Build the correct Python statement that creates a list called fruits containing three string items:
The Four Built-In Data Structures
Here is what you need to know about each one.
List
A list is an ordered, mutable collection. You can add items, remove items, and change items after the list is created. Lists use square brackets and allow any mix of data types — including other lists.
# Creating a list
fruits = ["apple", "banana", "cherry"]
# Accessing an item by index (zero-based)
print(fruits[0]) # apple
# Adding an item
fruits.append("mango")
print(fruits) # ['apple', 'banana', 'cherry', 'mango']
# Changing an item
fruits[1] = "blueberry"
print(fruits) # ['apple', 'blueberry', 'cherry', 'mango']
# Removing an item
fruits.remove("cherry")
print(fruits) # ['apple', 'blueberry', 'mango']
List indexing starts at 0, not 1. So the first item is at fruits[0], the second at fruits[1], and so on. This trips up many beginners at first — keep it in mind.
Tuple
A tuple is an ordered, immutable collection. Once a tuple is created, its contents cannot be changed. Tuples use parentheses and are appropriate for data that should stay fixed, such as coordinates, RGB colour values, or database records retrieved for read-only use.
# Creating a tuple
coordinates = (40.7128, -74.0060)
# Accessing items by index
print(coordinates[0]) # 40.7128
print(coordinates[1]) # -74.0060
# Tuples are immutable — this raises a TypeError:
# coordinates[0] = 51.5074
# Tuples can be unpacked into variables
latitude, longitude = coordinates
print(latitude) # 40.7128
Dictionary
A dictionary stores data as key-value pairs. Each key is unique and maps to a value. Dictionaries use curly braces with a colon separating each key and value. From Python 3.7 onwards, dictionaries maintain insertion order.
# Creating a dictionary
user = {
"name": "Jordan",
"age": 28,
"email": "jordan@example.com"
}
# Accessing a value by key
print(user["name"]) # Jordan
# Adding a new key-value pair
user["city"] = "Austin"
# Updating a value
user["age"] = 29
# Removing a key-value pair
del user["email"]
print(user)
# {'name': 'Jordan', 'age': 29, 'city': 'Austin'}
Set
A set is an unordered collection of unique values. Sets automatically reject duplicate entries and are very fast at checking whether a value is present. Sets use curly braces but contain single values rather than key-value pairs.
# Creating a set
permitted_roles = {"admin", "editor", "viewer"}
# Duplicates are silently removed
tags = {"python", "tutorial", "python", "beginner"}
print(tags) # {'python', 'tutorial', 'beginner'} — one 'python'
# Checking membership (very fast)
print("admin" in permitted_roles) # True
print("guest" in permitted_roles) # False
# Adding and removing items
permitted_roles.add("moderator")
permitted_roles.discard("viewer")
print(permitted_roles) # {'admin', 'editor', 'moderator'}
To create an empty set, write my_set = set(). Writing my_set = {} creates an empty dictionary, not a set. This is a frequent beginner mistake.
- Syntax
[item1, item2]- Ordered
- Yes — items keep their position
- Mutable
- Yes — add, remove, or change items
- Duplicates
- Allowed
- Best used for
- Sequences you need to modify — shopping carts, task queues, results lists
- Syntax
(item1, item2)- Ordered
- Yes — items keep their position
- Mutable
- No — contents are fixed after creation
- Duplicates
- Allowed
- Best used for
- Fixed data that should not change — coordinates, RGB values, function return values
- Syntax
{"key": value}- Ordered
- Yes (Python 3.7+) — insertion order preserved
- Mutable
- Yes — add, update, or delete key-value pairs
- Duplicates
- Keys must be unique; values can repeat
- Best used for
- Named data — user profiles, configuration settings, API response payloads
- Syntax
{item1, item2}orset()- Ordered
- No — no guaranteed order
- Mutable
- Yes — add or remove items
- Duplicates
- Not allowed — automatically removed
- Best used for
- Unique collections and fast membership tests — permissions, tags, visited URLs
Mutability: What Changes and What Does Not
The word "mutable" simply means changeable. Lists, dictionaries, and sets are all mutable — you can modify them after creation. Tuples are immutable — once you create a tuple, its values are locked in place.
Why does mutability matter? It affects how Python handles the object in memory, whether it can be used as a dictionary key, and how your code behaves when you pass data to functions. Tuples are also slightly faster to iterate than lists because Python does not need to account for the possibility of changes.
"The key question is not which structure is 'best' — it's which structure fits the shape of your data." — Common Python teaching principle
The code below tries to create an empty set, then add a value to it. One line has a bug. Click the line you think is wrong, then hit check.
{} to set(). Writing unique_items = {} creates an empty dictionary, not a set. Dictionaries do not have an .add() method, so line 2 would raise an AttributeError. Using set() is the only correct way to create an empty set in Python.
How to Choose the Right Data Structure in Python
When you are deciding which structure to use, walk through these four questions in order. The first answer that fits your situation points you to the right choice.
-
Decide whether order matters
If you need to access items by position — for example, the first result, the third item in a queue — you need an ordered structure. That points you toward a
listor atuple. If order is irrelevant, asetordictmay be a better fit. -
Decide whether the data will change
If the collection needs to grow, shrink, or be updated at runtime, use a mutable structure:
list,dict, orset. If the data is fixed and should be protected from modification — such as a set of constant configuration values — use atuple. -
Decide whether you need named keys
If each value has a meaningful label — such as
"name","age", or"status"— use adict. Accessing data by key is clearer and more self-documenting than accessing it by a numeric index like[2]. -
Decide whether uniqueness is required
If you need a collection where no value appears more than once — and you do not need positional access or named keys — use a
set. Sets are also the fastest of the four structures for testing whether a value is present in the collection.
Key Points to Remember
- Python's four built-in data structures are
list,tuple,dict, andset. Each one is suited to a different kind of problem. - Lists and tuples are ordered. Dictionaries maintain insertion order from Python 3.7 onwards. Sets have no guaranteed order.
- Lists, dictionaries, and sets are mutable (changeable). Tuples are immutable. To create an empty set, always use
set()— writing{}creates an empty dictionary instead.
These four structures show up in virtually every Python program. As you practise, you will develop an intuition for which one to reach for — but when in doubt, the four questions above give you a reliable way to reason through the decision.
Frequently Asked Questions
Python's four primary built-in data structures are list, tuple, dict (dictionary), and set. Each one organises data differently and is suited to different tasks.
Lists are mutable, meaning you can change their contents after creation. Tuples are immutable — once created, their values cannot be altered. Use a tuple when the data should not change, such as coordinates or configuration constants.
Use a dictionary when you need to look up values by a meaningful name (key) rather than by position. For example, storing a user's name, age, and email as a dictionary makes each value immediately identifiable without having to remember index numbers.
A set stores only unique values and has no guaranteed order. It cannot contain duplicates. This makes sets ideal for tasks like removing duplicates from a collection or checking membership very quickly.
Yes. A Python list can hold any mix of data types in the same collection — integers, strings, booleans, and even other lists or dictionaries. Python does not restrict a list to a single type.
Yes, from Python 3.7 onwards. Dictionaries maintain insertion order — items are stored and iterated in the order they were added. In older versions of Python (3.6 and below), dictionaries were unordered.
Empty list: my_list = []. Empty tuple: my_tuple = (). Empty dictionary: my_dict = {}. Empty set: my_set = set(). Note that {} creates an empty dictionary — you must use set() for an empty set.
Mutable means the object can be changed after it is created. Lists, dictionaries, and sets are mutable. Tuples are immutable — they cannot be changed after creation. Immutability makes tuples safer for fixed data and allows them to be used as dictionary keys.