Python Foundations

Collections in Python

A level 0 lesson on the main Python collections: list, tuple, set, frozenset, and dictionary.

0. What is a Collection?

A collection is a way to store many values inside one variable.

Without collections, we may write:

Without collection
student1 = "Amit"
student2 = "Riya"
student3 = "Karan"

print(student1)
print(student2)
print(student3)

With a collection, we can write:

With collection
students = ["Amit", "Riya", "Karan"]

print(students)
Simple meaning: A collection is a container for many values.
List Ordered and changeable collection.
Tuple Ordered but not changeable collection.
Set Unique values only. Duplicate values are removed.
Frozenset A set that cannot be changed.
Dictionary Stores data as key-value pairs.

1. List

A list is an ordered collection of values. Lists are written using square brackets: [].

Basic list
marks = [80, 75, 90]

Features of List

  • Ordered
  • Changeable
  • Allows duplicate values
  • Uses square brackets: []

List Example 1: Student Names

Python code
students = ["Amit", "Riya", "Karan"]

print(students)
print(students[0])
print(students[1])
print(students[2])
Output
['Amit', 'Riya', 'Karan']
Amit
Riya
Karan
Python indexing starts from 0. So students[0] means the first item.

List Example 2: Add and Change Values

Python code
fruits = ["apple", "banana", "mango"]

print("Original list:", fruits)

fruits.append("orange")
print("After adding orange:", fruits)

fruits[1] = "grapes"
print("After changing banana to grapes:", fruits)
Output
Original list: ['apple', 'banana', 'mango']
After adding orange: ['apple', 'banana', 'mango', 'orange']
After changing banana to grapes: ['apple', 'grapes', 'mango', 'orange']

2. Tuple

A tuple is also an ordered collection. Tuples are written using round brackets: ().

Basic tuple
point = (10, 20)

Features of Tuple

  • Ordered
  • Not changeable
  • Allows duplicate values
  • Uses round brackets: ()
Main difference: list can be changed, but tuple cannot be changed.

Tuple Example 1: Coordinates

Python code
point = (10, 20)

print(point)
print("x =", point[0])
print("y =", point[1])
Output
(10, 20)
x = 10
y = 20

Tuple Example 2: Days of Week

Python code
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

print(days)
print(days[0])
print(days[4])
Output
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
Monday
Friday

What happens if we try to change a tuple?

This gives an error
days = ("Monday", "Tuesday", "Wednesday")

days[0] = "Sunday"
Error
TypeError: 'tuple' object does not support item assignment

3. Set

A set is a collection of unique values. Sets are written using curly brackets: {}.

Basic set
numbers = {1, 2, 3}

Features of Set

  • Unordered
  • Changeable
  • Does not allow duplicate values
  • Uses curly brackets: {}

Set Example 1: Remove Duplicates Automatically

Python code
numbers = {1, 2, 3, 2, 1, 4}

print(numbers)
Output may be
{1, 2, 3, 4}
A set keeps only unique values. Duplicate values are removed automatically.

Set Example 2: Add and Remove Items

Python code
subjects = {"Python", "Java", "SQL"}

print("Original set:", subjects)

subjects.add("HTML")
print("After adding HTML:", subjects)

subjects.remove("Java")
print("After removing Java:", subjects)
Output may be
Original set: {'Python', 'Java', 'SQL'}
After adding HTML: {'Python', 'Java', 'SQL', 'HTML'}
After removing Java: {'Python', 'SQL', 'HTML'}
The output order may be different because sets are unordered.

4. Frozenset

A frozenset is like a set, but it cannot be changed.

Basic frozenset
numbers = frozenset([1, 2, 3])

Features of Frozenset

  • Unordered
  • Not changeable
  • Does not allow duplicate values
  • Created using frozenset()
Think of a frozenset as a frozen set. It is locked after creation.

Frozenset Example 1: Create a Frozenset

Python code
numbers = frozenset([1, 2, 3, 2, 1])

print(numbers)
Output
frozenset({1, 2, 3})

Frozenset Example 2: Check Membership

Python code
subjects = frozenset(["Python", "Java", "SQL"])

print("Python" in subjects)
print("HTML" in subjects)
Output
True
False

What happens if we try to add to a frozenset?

This gives an error
subjects = frozenset(["Python", "Java", "SQL"])

subjects.add("HTML")
Error
AttributeError: 'frozenset' object has no attribute 'add'

5. Dictionary

A dictionary stores data in key-value pairs.

Basic dictionary
student = {
    "name": "Amit",
    "age": 15,
    "marks": 88
}

Each item has a key and a value:

Key-value pair
"name": "Amit"
  • "name" is the key
  • "Amit" is the value

Features of Dictionary

  • Ordered
  • Changeable
  • Does not allow duplicate keys
  • Stores key-value pairs

Dictionary Example 1: Student Data

Python code
student = {
    "name": "Amit",
    "age": 15,
    "marks": 88
}

print(student)
print(student["name"])
print(student["age"])
print(student["marks"])
Output
{'name': 'Amit', 'age': 15, 'marks': 88}
Amit
15
88

Dictionary Example 2: Add and Update Values

Python code
student = {
    "name": "Riya",
    "age": 14,
    "marks": 91
}

print("Original dictionary:", student)

student["city"] = "Varanasi"
print("After adding city:", student)

student["marks"] = 95
print("After updating marks:", student)
Output
Original dictionary: {'name': 'Riya', 'age': 14, 'marks': 91}
After adding city: {'name': 'Riya', 'age': 14, 'marks': 91, 'city': 'Varanasi'}
After updating marks: {'name': 'Riya', 'age': 14, 'marks': 95, 'city': 'Varanasi'}

Comparison Table

Collection Brackets / Syntax Ordered Changeable Allows Duplicates Example
List [] Yes Yes Yes [1, 2, 2, 3]
Tuple () Yes No Yes (1, 2, 2, 3)
Set {} No Yes No {1, 2, 3}
Frozenset frozenset() No No No frozenset([1, 2, 3])
Dictionary {key: value} Yes Yes Keys cannot repeat {"name": "Amit"}

One Program Using All Collections

Python code
# List
students = ["Amit", "Riya", "Karan"]

# Tuple
days = ("Monday", "Tuesday", "Wednesday")

# Set
unique_marks = {80, 90, 80, 75, 90}

# Frozenset
fixed_subjects = frozenset(["Python", "Maths", "English"])

# Dictionary
student_details = {
    "name": "Amit",
    "age": 15,
    "marks": 88
}

print("List:", students)
print("Tuple:", days)
print("Set:", unique_marks)
print("Frozenset:", fixed_subjects)
print("Dictionary:", student_details)
Output may be
List: ['Amit', 'Riya', 'Karan']
Tuple: ('Monday', 'Tuesday', 'Wednesday')
Set: {80, 90, 75}
Frozenset: frozenset({'Python', 'Maths', 'English'})
Dictionary: {'name': 'Amit', 'age': 15, 'marks': 88}

When Should We Use What?

Use List When you want many values, want order, and want to change the values later.
shopping_list = ["rice", "dal", "milk"]
Use Tuple When you want fixed values that should not change.
rgb_color = (255, 0, 0)
Use Set When you want unique values only.
roll_numbers = {1, 2, 3, 4}
Use Frozenset When you want unique values that should not change.
allowed_subjects = frozenset(["Python", "Java", "SQL"])
Use Dictionary When you want to store meaningful records with names.
student = {
    "name": "Amit",
    "class": 9,
    "city": "Varanasi"
}

Practice Questions

Question 1

Create a list of 5 fruits and print the second fruit.

Solution
fruits = ["apple", "banana", "mango", "orange", "grapes"]

print(fruits[1])

Question 2

Create a tuple of 3 numbers and print the last number.

Solution
numbers = (10, 20, 30)

print(numbers[2])

Question 3

Create a set with duplicate numbers and print it.

Solution
numbers = {1, 2, 2, 3, 3, 4}

print(numbers)

Question 4

Create a frozenset of subjects and check whether "Python" is present.

Solution
subjects = frozenset(["Python", "Java", "SQL"])

print("Python" in subjects)

Question 5

Create a dictionary for a student with name, age, and marks.

Solution
student = {
    "name": "Riya",
    "age": 14,
    "marks": 92
}

print(student)
print(student["name"])
print(student["marks"])

Mini Project: Student Record System

This mini project combines list, dictionary, set, and tuple.

Python code
students = [
    {
        "name": "Amit",
        "age": 15,
        "subjects": {"Python", "Maths", "English"},
        "marks": (88, 76, 92)
    },
    {
        "name": "Riya",
        "age": 14,
        "subjects": {"Python", "Science", "English"},
        "marks": (91, 85, 89)
    }
]

for student in students:
    print("Name:", student["name"])
    print("Age:", student["age"])
    print("Subjects:", student["subjects"])
    print("Marks:", student["marks"])
    print("Total marks:", sum(student["marks"]))
    print()
Output
Name: Amit
Age: 15
Subjects: {'Python', 'Maths', 'English'}
Marks: (88, 76, 92)
Total marks: 256

Name: Riya
Age: 14
Subjects: {'Python', 'Science', 'English'}
Marks: (91, 85, 89)
Total marks: 265

Final Memory Trick

Collection Memory Trick
List Changeable line of items
Tuple Fixed line of items
Set Unique items
Frozenset Fixed unique items
Dictionary Key-value data
Best beginner rule: Use list for simple collections. Use dictionary for meaningful records. Use set when duplicates must disappear. Use tuple or frozenset when data should not change.

Try the Code in Our Python Editor

Use the embedded Python editor below to run and modify the examples.