When working with Python, data structures are the foundation of how you organize and manage information. Among the most commonly used built-in types are list, tuple, set, and dictionary.
Each of them serves a unique purpose and understanding their differences helps you write cleaner, more efficient, and maintainable code.
In this post, we’ll break down their properties, use cases, and key differences — with simple examples.
1. List — The Flexible Ordered Collection
A list is an ordered, changeable (mutable) collection that can hold multiple items. It allows duplicate elements and is one of the most versatile structures in Python.
Syntax:
numbers = [10, 20, 30, 40]
Key Features:
- Ordered — elements have a defined position.
- Mutable — you can change, add, or remove items.
- Allows duplicates.
- Accessible by index.
Example:
numbers[1] = 99
print(numbers) # Output: [10, 99, 30, 40]
Best for: When you need an ordered and modifiable collection of items.
2. Tuple — The Immutable Sequence
A tuple is similar to a list but immutable, meaning once created, its content cannot be changed. It’s often used for fixed data that shouldn’t be modified accidentally.
Syntax:
data = (10, 20, 30)
Key Features:
- Ordered and indexed.
- Immutable (cannot be modified).
- Allows duplicates.
Example:
print(data[1]) # Output: 20
# data[1] = 99 ❌ — will raise an error
Best for: Storing constant or read-only data (like coordinates or configuration values).
3. Set — The Collection of Unique Elements
A set is an unordered collection of unique items. It automatically removes duplicates and provides powerful mathematical operations like union and intersection.
Syntax:
nums = {10, 20, 30, 10}
print(nums) # Output: {10, 20, 30}
Key Features:
- Unordered and unindexed.
- Mutable.
- No duplicate items.
- Supports set operations.
Example:
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # Union → {1, 2, 3, 4, 5}
print(a & b) # Intersection → {3}
Best for: Storing unique values or performing mathematical set operations.
4. Dictionary — Key-Value Data Mapping
A dictionary stores data in key-value pairs, allowing fast access to values through their keys. It’s ordered (from Python 3.7+) and mutable.
Syntax:
student = {"name": "Jubayer", "age": 22, "city": "Dhaka"}
Key Features:
- Ordered by insertion (Python 3.7+).
- Mutable.
- Keys are unique, but values can repeat.
- Access via keys, not indexes.
Example:
print(student["name"]) # Output: Jubayer
student["age"] = 23 # Update value
Best for: Representing structured data or real-world objects with labeled attributes.
Quick Comparison Table
| Feature | List | Tuple | Set | Dictionary |
|---|---|---|---|---|
| Ordered | ✅ | ✅ | ❌ | ✅ |
| Mutable | ✅ | ❌ | ✅ | ✅ |
| Duplicates | ✅ | ✅ | ❌ | Keys ❌, Values ✅ |
| Indexed | ✅ | ✅ | ❌ | By key |
| Syntax | [] |
() |
{} |
{key: value} |
Final Thoughts
Choosing the right data structure can make your Python code more efficient and easier to understand:
- Use List when you need an ordered and flexible collection.
- Use Tuple when your data must remain constant.
- Use Set when you need unique elements or mathematical operations.
- Use Dictionary when you want to map relationships between keys and values.
Understanding these differences is a core skill for every Python developer — whether you’re building a simple script or designing a large-scale application.