R
Rishtaara
Python: Zero to Professional
Lesson 5 of 40Article20 min

Lists, Tuples, Sets & Dictionaries

Lists are ordered, mutable collections in square brackets []. Methods: append, extend, insert, remove, pop, sort.

Python Lists

Lists are ordered, mutable collections in square brackets []. Methods: append, extend, insert, remove, pop, sort.

Lists can hold mixed types and are the default dynamic array in Python.

Real-life example: A list is a shopping list — you can add, remove, and reorder items anytime.

List operations
skills = ["python", "sql"]
skills.append("git")
skills[0] = "Python"
print(skills, len(skills))

Python Tuples

Tuples use parentheses () and are ordered but immutable — good for fixed records like coordinates.

Unpacking lets you assign multiple variables at once.

Real-life example: A tuple is a sealed exam paper — you can read it but not erase answers.

Tuple unpacking
point = (10, 20)
x, y = point
print(x, y)

Python Sets

Sets store unique unordered items in curly braces {}. Great for removing duplicates and membership tests.

Set operations include union (|), intersection (&), and difference (-).

Real-life example: A set is a bag of unique marbles — duplicate colors automatically disappear.

Unique tags
tags = {"api", "backend", "api"}
print(tags)  # {'api', 'backend'}

Python Dictionaries

Dicts map keys to values with {}. Keys must be hashable (usually strings or numbers).

Use .get() for safe access, .keys(), .values(), .items() for loops.

Real-life example: A dictionary is a phone contact book — name (key) points to number (value).

Student record
student = {"name": "Riya", "score": 92}
student["score"] = 95
print(student.get("name"), student["score"])