Python Collections Explained

Learn about Python's four main collection types, lists, tuples, sets, and dictionaries, and understand which one to use for any programming task.

5 min read

Python collections are the built-in types that let you store and organize multiple values inside a single variable. Up to this point you have worked with individual values, a single integer, a single string, a single float. Real programs rarely deal with one piece of data at a time. You need to hold a list of user names, a set of unique product IDs, a mapping from country codes to country names, or a sequence of sensor readings collected over time. Python gives you four collection types to handle each of these situations: lists, tuples, sets, and dictionaries. This article explains what each type is for and how they compare.

Each of these four types solves a different organizational problem, and the skill of choosing the right one for the task is something you develop with practice. A list keeps items in order and lets you change them. A tuple also keeps items in order but locks them so they cannot be changed. A set throws away duplicates and lets you test membership almost instantly. A dictionary pairs keys with values and retrieves any value by its key in constant time regardless of how many entries it holds. The articles in this section walk through each type step by step, starting with working with Python lists right after this overview.

Why collections matter

Before Python had variables that could hold multiple values, you would have needed a separate variable for every piece of data in your program. Ten user names meant ten variables. A hundred sensor readings meant a hundred variables. That approach does not scale beyond toy examples, and it makes loops, searches, and data transformations impossible to express cleanly.

A collection solves this by acting as a single container that holds many items. You give the container one name, then access individual items by position, by key, or by iteration. Python's for loop works directly with every collection type, which means you can process thousands of items with the same three-line pattern whether they live in a list, a tuple, a set, or the keys of a dictionary. This uniformity is one of the reasons Python feels productive even when you are handling large amounts of data.

Collections also define the boundaries between parts of your program. A function that processes a list of orders can be tested with a small hand-crafted list, then deployed against a list of ten thousand real orders without changing a single line of the function body. The collection type abstracts away the count, so your logic focuses on what to do with each item rather than on how many items exist.

The four collection types at a glance

The table below summarizes the key properties of each collection type. Keep this comparison in mind as you read the detailed articles that follow.

TypeOrdered?Mutable?Duplicates?Syntax example
listYesYesYes[1, 2, 3]
tupleYesNoYes(1, 2, 3)
setNoYesNo{1, 2, 3}
dictYes (keys)YesKeys: no, Values: yes{"a": 1, "b": 2}

Lists and tuples are sequence types, which means you can index into them with square brackets and slice them with the colon operator. Sets and dictionaries are not sequences; you cannot access them by numeric position, but you can test whether a value or key exists inside them with the in operator, and both support fast membership testing that does not slow down as the collection grows.

Lists: the ordered, changeable workhorse

A Python list holds items in a specific order, and that order stays the same until you explicitly rearrange it. You add items, remove them, and change any item by assigning to its index. Because lists are mutable, you can build them incrementally: start with an empty list, loop through some data source, and grow the list one item at a time.

Lists are also the only collection type that supports sorting. Both the sort method and the sorted built-in function work on lists, and the article on sorting and reversing Python lists later in this section covers every technique you need as a beginner. Lists are the right choice when the sequence of items carries meaning, a timeline of events, a ranked list of scores, or steps in a recipe where the order matters.

One thing to watch for with lists is that they are passed by reference. When you assign a list to a new variable, both variables point to the same list object in memory. Changes made through one variable are visible through the other. The article on copying Python lists explains shallow and deep copies so you can avoid accidental data sharing.

Tuples: the ordered, immutable record

A tuple looks like a list at first glance because both use sequential storage and support indexing and slicing. The critical difference is that a tuple cannot be changed after it is created. You cannot append to a tuple, remove from a tuple, or reassign an element at a given index. This immutability is not a limitation; it is a guarantee that makes tuples the right choice when you want to ensure that a group of values stays exactly as you defined it.

Tuples are commonly used to represent fixed records. A row from a database, a coordinate pair, an RGB color triple, these are values that should not be accidentally modified. Python itself uses tuples extensively. When a function returns multiple values separated by commas, what you actually get back is a tuple. The multiple assignment syntax works because the right side is a tuple being unpacked into the variables on the left.

Because tuples are immutable and hashable, they can serve as keys in dictionaries and as elements in sets. A list cannot be a dictionary key because it could change, which would break the dictionary's internal hash table. The article on working with Python tuples covers creation, packing, unpacking, and the scenarios where tuples outperform lists.

Sets: unique elements and fast membership

A set stores an unordered collection of unique items. Duplicates are silently discarded when the set is created, and attempting to add an item that already exists has no effect. This property makes sets the natural tool for removing duplicates from a list: pass the list to the set constructor, and you get back a collection with only the distinct values.

Sets are optimized for membership testing. The expression that checks whether a value belongs to a set runs in constant time regardless of the set's size, because sets use the same hash-table internals that dictionaries use for their keys. If your program repeatedly checks whether items belong to a known group, checking words against a dictionary of valid terms, or filtering IDs against an allowlist, storing that group in a set rather than a list can make the difference between a program that finishes in milliseconds and one that takes minutes.

Sets also support mathematical operations that lists and tuples do not. You can compute the union of two sets, their intersection, and their difference. The article on set operations in Python demonstrates each of these with practical examples like finding common elements between two data sources.

Dictionaries: key-value lookups

A dictionary maps unique keys to values, and it retrieves any value by its key in the same fast, constant time that sets provide for membership testing. You create a dictionary by writing key-value pairs inside curly braces, separated by colons. The keys must be immutable, strings, numbers, and tuples are common choices, while the values can be any Python object, including other collections.

Dictionaries are the backbone of data processing in Python. When you parse JSON from a web API, the result is a dictionary. When you count word frequencies in a text, a dictionary naturally maps each word to its count. When you configure a program with named parameters, a dictionary holds the configuration. The dict type is so central to the language that Python's keyword arguments and its internal namespace system both rely on dictionary-like mappings.

Dictionaries preserve insertion order as of Python 3.7, which means the order you add keys is the order you get back when you iterate. This was an implementation detail in Python 3.6 and became a language guarantee in 3.7. You can rely on it when you need predictable iteration order, though for most dictionary use cases the order is irrelevant and the key is what matters.

How this section is organized

The next several articles focus on lists, because lists are the collection type you will use most often as a beginner. You will learn what lists are and how they differ from other types, then move into creating and accessing Python lists with square-bracket syntax and index positions. From there you will learn to modify lists by adding, removing, and changing items, followed by sorting and reversing.

After the list articles, the section covers tuples, sets, and dictionaries in turn. Each type gets its own cluster of articles that start with an overview and then dive into creation, modification where applicable, and common operations. The section closes with cross-cutting articles on iteration, membership testing, built-in collection functions, performance considerations, and common mistakes that trip up beginners.

Rune AI

Rune AI

Key Insights

  • Python has four built-in collection types: list (ordered, mutable), tuple (ordered, immutable), set (unordered, unique), and dict (key-value mapping).
  • Lists are the general-purpose workhorse; use them when you need an ordered, changeable sequence.
  • Tuples protect data from accidental modification and enable use as dictionary keys.
  • Sets automatically remove duplicates and support fast membership testing.
  • Dictionaries map unique keys to values and are the fastest way to look up data by a known identifier.
RunePowered by Rune AI

Frequently Asked Questions

What are the four main Python collection types?

The four built-in collection types in Python are lists, tuples, sets, and dictionaries. Lists are ordered and mutable, tuples are ordered and immutable, sets are unordered with unique elements, and dictionaries store key-value pairs with fast key-based lookups.

How do I decide between a list and a tuple?

Use a list when you need a collection that will change over time, adding items, removing items, or reordering. Use a tuple when the collection represents a fixed group of values, like coordinates or a database row, and when you want the safety guarantee that the data will not be modified accidentally.

When should I use a set instead of a list?

Use a set when you need to eliminate duplicate values, test membership quickly, or perform mathematical set operations like union and intersection. Sets do not preserve insertion order and require all elements to be hashable, so they are not a drop-in replacement for lists.

Conclusion

Python's four collection types, lists, tuples, sets, and dictionaries, give you a complete toolkit for organizing data. Learning when to choose each one and understanding their core behaviors is the foundation that every subsequent programming task builds on.