Chapter 7: Lists, Tuples, Dictionaries & Sets
Learn how Python stores multiple values using collection data types and how to manage student records, course lists, unique values, and structured information.
[ ]
( )
{key:value}
{ }
7.1 Chapter Overview
In earlier chapters, learners used variables to store single values such as one name, one mark, or one course fee. However, real applications usually need to store many values together. Python provides collection data types such as lists, tuples, dictionaries, and sets to manage multiple values efficiently.
These data structures are useful in student registration systems, marks processing, course management, attendance tracking, inventory systems, and data analysis applications.
7.2 Chapter Objectives
- Understand the purpose of Python collection data types.
- Create and modify lists.
- Use tuples for fixed data.
- Create dictionaries using key-value pairs.
- Use sets to store unique values.
- Apply loops to process collections.
- Develop simple Python applications using collections.
Learning Outcomes
- Differentiate between lists, tuples, dictionaries, and sets.
- Store multiple values in Python collections.
- Access, update, add, and remove collection items.
- Use collections to build practical student and course management programs.
7.3 What are Collection Data Types?
Collection data types are used to store multiple values in a single variable. Each collection type has its own purpose, structure, and behavior.
| Collection | Symbol | Main Feature | Example |
|---|---|---|---|
| List | [ ] | Ordered and changeable. | ["Python", "AI", "Data Science"] |
| Tuple | ( ) | Ordered but not changeable. | ("Monday", "Tuesday") |
| Dictionary | { } | Stores key-value pairs. | {"name": "Amin", "age": 20} |
| Set | { } | Stores unique unordered values. | {"Python", "AI", "Python"} |
7.4 Lists in Python
A list is used to store multiple values in one variable. Lists are ordered, changeable, and allow duplicate values.
Creating a List
courses = ["Python", "AI", "Data Science", "Cyber Security"] print(courses)
['Python', 'AI', 'Data Science', 'Cyber Security']
Accessing List Items
List items are accessed using index numbers. Python indexing starts from 0.
courses = ["Python", "AI", "Data Science"] print(courses[0]) print(courses[1])
Python
AI
7.5 Modifying Lists
Lists are changeable, so items can be updated, added, and removed.
Update an Item
courses = ["Python", "AI", "Data Science"] courses[1] = "Machine Learning" print(courses)
Add an Item
courses = ["Python", "AI"]
courses.append("Cyber Security")
print(courses)
Remove an Item
courses = ["Python", "AI", "Data Science"]
courses.remove("AI")
print(courses)
7.6 Looping Through Lists
A for loop can be used to process each item in a list.
students = ["Amin", "Mei Ling", "Ravi", "Siti"]
for student in students:
print("Student Name:", student)
Student Name: Amin
Student Name: Mei Ling
Student Name: Ravi
Student Name: Siti
7.7 Tuples in Python
A tuple is similar to a list, but it cannot be changed after it is created. Tuples are useful for fixed data.
Creating a Tuple
weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
print(weekdays)
Accessing Tuple Items
weekdays = ("Monday", "Tuesday", "Wednesday")
print(weekdays[0])
Monday
7.8 When to Use Tuples
Tuples are best used when data should remain fixed and protected from accidental changes.
- Days of the week
- Months of the year
- Fixed course codes
- Permanent system settings
- Coordinates or fixed values
course_codes = ("CPP101", "AI201", "DS301")
for code in course_codes:
print(code)
7.9 Dictionaries in Python
A dictionary stores data in key-value pairs. Each value is accessed using its key.
Creating a Dictionary
student = {
"name": "Amin",
"age": 20,
"course": "Certified Python Programmer"
}
print(student)
Accessing Dictionary Values
student = {
"name": "Amin",
"age": 20,
"course": "Python"
}
print(student["name"])
print(student["course"])
Amin
Python
7.10 Updating Dictionaries
Update a Value
student = {
"name": "Amin",
"age": 20,
"course": "Python"
}
student["course"] = "AI Application Development"
print(student)
Add a New Key-Value Pair
student = {
"name": "Amin",
"age": 20
}
student["email"] = "amin@example.com"
print(student)
Loop Through Dictionary
student = {
"name": "Amin",
"age": 20,
"course": "Python"
}
for key, value in student.items():
print(key, ":", value)
7.11 Sets in Python
A set is used to store unique values. Sets are unordered and do not allow duplicate items.
Creating a Set
skills = {"Python", "AI", "Data Science", "Python"}
print(skills)
The duplicate value "Python" will be stored only once.
Add Item to Set
skills = {"Python", "AI"}
skills.add("Cyber Security")
print(skills)
Remove Item from Set
skills = {"Python", "AI", "Data Science"}
skills.remove("AI")
print(skills)
7.12 Set Operations
Sets are useful when comparing groups of values.
Union
group_a = {"Python", "AI"}
group_b = {"Data Science", "AI"}
all_skills = group_a.union(group_b)
print(all_skills)
Intersection
group_a = {"Python", "AI"}
group_b = {"Data Science", "AI"}
common_skills = group_a.intersection(group_b)
print(common_skills)
7.13 Comparison of Python Collections
| Feature | List | Tuple | Dictionary | Set |
|---|---|---|---|---|
| Ordered | Yes | Yes | Yes | No |
| Changeable | Yes | No | Yes | Yes |
| Allows Duplicates | Yes | Yes | No duplicate keys | No |
| Best Use | Changing lists of data | Fixed data | Structured records | Unique values |
7.14 Practical Example: Student Record System
This example uses a dictionary to store student details and a list to store marks.
student = {
"name": "Amin",
"course": "Certified Python Programmer",
"marks": [80, 75, 90]
}
total = 0
for mark in student["marks"]:
total = total + mark
average = total / len(student["marks"])
print("Student Name:", student["name"])
print("Course:", student["course"])
print("Total Marks:", total)
print("Average Marks:", average)
Student Name: Amin
Course: Certified Python Programmer
Total Marks: 245
Average Marks: 81.66666666666667
7.15 Common Beginner Mistakes
| Mistake | Problem | Correction |
|---|---|---|
| Using wrong brackets | Confusing lists, tuples, dictionaries and sets. | Use [ ] for lists, ( ) for tuples, and { } for dictionaries/sets. |
| Changing tuple values | Tuples cannot be modified. | Use a list if values need to change. |
| Using duplicate keys in dictionary | Duplicate keys overwrite previous values. | Use unique keys. |
| Expecting set order | Sets are unordered. | Use a list if order is important. |
7.16 Hands-On Practice
Activity 1: Course List
Create a list of five courses. Print each course using a for loop.
Activity 2: Fixed Course Codes
Create a tuple containing course codes. Display all course codes.
Activity 3: Student Dictionary
Create a dictionary that stores student name, age, course, and email. Display each value clearly.
Activity 4: Unique Skills Set
Create a set of student skills and add one new skill using the add() method.
Mini Project: Student Profile Manager
Create a Python program that stores student details in a dictionary, course names in a list, fixed centre information in a tuple, and unique skills in a set.
7.17 Final Assessment Quiz
Answer the following questions. Correct Answer = +1 Mark Wrong Answer = -0.5 Mark
1. A list can store multiple values in one variable.
2. Tuples can be changed after creation.
3. Dictionaries store data using key-value pairs.
4. Sets allow duplicate values.
5. Python list indexing starts from 0.
6. The append() method can add a new item to a list.
7. A tuple is suitable for fixed values that should not change.
8. Dictionary values are accessed using keys.
9. Sets are useful for storing unique values.
10. A dictionary can be useful for storing a student profile.
Your Score: 0
Final Practical Assessment
Develop a Python program named student_collection_manager.py. The program must use a list for courses, a tuple for fixed centre details, a dictionary for one student profile, and a set for unique skills. Display all data clearly using loops where suitable.
7.18 Chapter Summary
In this chapter, learners studied Python collection data types: lists, tuples, dictionaries, and sets. Lists are flexible and changeable, tuples are fixed, dictionaries store key-value records, and sets store unique values.