Python Data Structures – Lists, Tuples, Sets, and Dictionaries

August 02, 2025   5 Min Read 35

What You’ll Learn:

  • Built-in data structures in Python
  • Differences between them
  • When to use which
  • Methods and operations
  • Real-life examples
  • Practice problems

Why Data Structures?

Data structures help you organize and store data efficiently so that operations like insertion, deletion, searching, and sorting become easier.

Python has four main built-in data structures:

  • List
  • Tuple
  • Set
  • Dictionary

1. Lists – Ordered, Mutable Collections

Key Features:

  • Ordered
  • Mutable (can be changed)
  • Allows duplicate elements

Creating a List:


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

Accessing Items:


print(fruits[0])       # Output: apple
print(fruits[-1])      # Output: cherry (last item)

# Add
fruits.append("kiwi")

# Insert at position
fruits.insert(1, "grape")

# Delete
fruits.remove("banana")  # Removes by value
del fruits[2]  # Rmmoves by index

Useful Methods:

  • append(), extend(), insert(), remove(), pop(), sort(), reverse(), count(), index()

Modifying a List:


fruits[1] = "blueberry"
print(fruits)  # ['apple', 'blueberry', 'cherry']

List Methods:

MethodDescription
append()Add element to end
insert(index, x)Insert at specific position
remove(x)Remove first occurrence of x
pop()Remove and return last item
sort()Sort list
reverse()Reverse the list

numbers = [3, 1, 4]
numbers.append(2)
numbers.sort()
print(numbers)  # [1, 2, 3, 4]

2. Tuples – Ordered, Immutable Collections

Key Features:

  • Ordered
  • Immutable (cannot be changed)
  • Faster than lists

Creating a Tuple:


person = ("Akash", 25, "India")

Accessing Elements:


print(person[0])  # Akash

Use tuples when data shouldn’t change (e.g., coordinates, constants).

3. Sets – Unordered, Unique Elements

Key Features:

  • Unordered
  • No duplicates
  • Mutable (can be changed)
  • Useful for mathematical set operations

Creating a Set:


colors = {"red", "green", "blue"}

Set Operations:


colors.add("yellow")
colors.remove("green")

Set Math:


a = {1, 2, 3}
b = {3, 4, 5}

print(a | b)  # Union → {1, 2, 3, 4, 5}
print(a & b)  # Intersection → {3}
print(a - b)  # Difference → {1, 2}

Great for checking membership, uniqueness, and eliminating duplicates.

4. Dictionaries – Key-Value Pairs

Key Features:

  • Unordered (as of < Python 3.6; ordered from 3.7+)
  • Key-value mapping
  • Fast lookups

Creating a Dictionary:


student = {
    "name": "Akash",
    "age": 25,
    "course": "Python"
}

Accessing & Modifying:


print(student["name"])     # Akash
student["age"] = 26        # Update
student["email"] = "a@x.com"  # Add new key

Dictionary Methods:

MethodDescription
keys()All keys
values()All values
items()All key-value pairs
get(key)Get value by key safely
pop(key)Remove item by key

Summary: When to Use What?

StructureOrderedMutableDuplicatesUse When…
ListYesYesYesOrder matters, frequent updates
TupleYesNoYesFixed data, safety
SetNoYesNoUniqueness, fast membership tests
DictionaryYes*YesKeys uniqueMap keys to values

Real-World Examples

List Example – Shopping Cart


cart = ["apple", "banana", "apple"]
print(cart.count("apple"))  # Output: 2

Tuple Example – Coordinates


location = (37.7749, -122.4194)

Set Example – Unique Voters


voters = {"alice", "bob", "alice"}
print(voters)  # Output: {'alice', 'bob'}

Dictionary Example – API Response


response = {
    "status": "success",
    "data": {"user_id": 101, "name": "Akash"}
}

Practice Tasks

✅ Task 1: List Practice

  • Create a list of your 5 favorite movies
  • Add 2 more
  • Sort the list
  • Remove the last movie

✅ Task 2: Set Practice

  • Create a set of integers with duplicates
  • Remove duplicates and print the sorted result

✅ Task 3: Dictionary Practice

  • Create a dictionary for a book:
    • title, author, year, price
  • Update the price
  • Add key "available": True

Mini Quiz

  1. Can a dictionary key be a list? Why or why not?
  2. What happens if you access a missing key using dict[key] vs dict.get(key)?
  3. Which is faster for checking membership: list or set?

Leave a Reply