Python Mastery – From Basics to Brilliance / Data Structures in Python

List vs Tuple vs Set vs Dictionary

πŸ”Ή Lists πŸ“œ

  • βœ… Ordered, βœ… Mutable, βœ… Allows Duplicates

  • πŸ”§ Modify using:

    append(), extend(), insert(), remove(), pop()

fruits = ["apple", "banana", "cherry"]

πŸ“Œ Use when order matters and the data may change.


🧱 Tuples

  • βœ… Ordered, ❌ Immutable, βœ… Allows Duplicates

  • ⚑ Faster than lists, safe from accidental modification

coordinates = (10.0, 20.5)

πŸ“Œ Use when data should not change (e.g., configuration, coordinates).


πŸ”Ή Sets πŸ”‚

  • ❌ Unordered, βœ… Mutable, ❌ No Duplicates

  • βš™οΈ Supports operations like union(), intersection(), difference()

unique_numbers = {1, 2, 3}

πŸ“Œ Use for uniqueness, fast membership tests, and removing duplicates.


πŸ“š Dictionaries πŸ”‘

  • πŸ”‘ Key-value pairs, βœ… Mutable, βœ… No duplicate keys

  • Fast lookup using keys

student = {"name": "Alice", "age": 21}

πŸ“Œ Use when associating keys with values (like database records or API responses).


πŸ” List vs Tuple vs Set – Quick Comparison

Feature

List

Tuple

Set

Dictionary

Syntax

[ ]

( )

{ }

{key: value}

Example

[1, 2, 3]

(1, 2, 3)

{1, 2, 3}

{"a": 1, "b": 2}

Ordered

βœ… Yes

βœ… Yes

❌ No*

βœ… Yes**

Mutable

βœ… Yes

❌ No

βœ… Yes

βœ… Yes

Allows duplicates

βœ… Yes

βœ… Yes

❌ No

Keys ❌, Values βœ…

Indexing

βœ… Yes

βœ… Yes

❌ No

❌ By index

Key-Value pairs

❌ No

❌ No

❌ No

βœ… Yes

Can be changed after creation

βœ… Yes

❌ No

βœ… Yes

βœ… Yes

Use when

You need an ordered, changeable collection

You need an ordered, unchangeable collection

You need unique items

You need data in key-value form


βœ… Tip:

  • Lists for general-purpose arrays and sequences.

  • Tuples when data must remain unchanged.

  • Sets when duplicates must be eliminated and membership tested quickly.

  • Dictionaries when data is associated with named identifiers.