Skip to content

Data Structure Comparisons

Choosing the right collection type is crucial. Here is a summary of the four built-in collection types in Python.

Summary Table

Type Ordered? Changeable? Duplicates? Use Case
List Yes Yes Yes General-purpose collection.
Tuple Yes No Yes Data that shouldn't change (Safe).
Set No Yes* No Unique items, math operations.
Dict Yes Yes No (Keys) Mapping labels to values (Profiles).

*Set items are unchangeable, but you can add/remove items.

Quick Selection Guide

  1. Do you need to store items with a specific order and might change them?
    • Use a List.
  2. Do you have data that should stay constant (like months of the year)?
    • Use a Tuple.
  3. Do you need to ensure every item is unique?
    • Use a Set.
  4. Do you need to look up a value using a name or label?
    • Use a Dictionary.

Example: Storing a Student

# List: Good for grades
grades = [85, 90, 78]

# Tuple: Good for fixed ID and Name
student_info = (101, "Vishnu")

# Set: Good for unique courses
courses = {"Python", "Java", "SQL"}

# Dictionary: Best for a full profile
profile = {
    "id": 101,
    "name": "Vishnu",
    "active": True
}