Complete Python Series · Part 20 of 29
Assignment 07-enrollment-manager
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO Student Enrollment Manager
Learn to remove duplicate elements from a list using sets, convert collections between types, append new elements, and sort a list alphabetically.
Problem statement
NextGen CTO collects student enrollment records from multiple sources, including website registrations, college bootcamps and promotional campaigns. The resulting list may contain duplicate student names.
Build a simple enrollment management system that removes duplicate names, adds a newly enrolled student named Payal, and displays the final list in alphabetical order.
Task requirements
- Convert the raw student list into a set to remove duplicates.
- Convert the set back into a list.
- Append the new student
Payal. - Sort the list alphabetically.
- Print the final list.
Constraints
- Use built-in Python lists and sets.
- Use
set(),list(),append()andsort(). - Do not use loops or conditional statements.
- Assume each name represents a distinct student in this simulation.
Good luck trying the exercise!
Starter code
raw_students = [
"Aman", "Priya", "Rahul", "Aman", "Neha",
"Priya", "Ishan", "Rahul", "Avesh"
]
# TODO: Remove duplicate student names
# TODO: Convert the set back into a list
# TODO: Add the newly enrolled student "Payal"
# TODO: Sort the student list alphabetically
# TODO: Print the final list
Expected output
['Aman', 'Avesh', 'Ishan', 'Neha', 'Payal', 'Priya', 'Rahul']
Hints
- A set stores unique elements. Use
set()to remove duplicates. - Use
list()to convert a set into a list. - Use
append()to add an element to the end of a list. - Use
sort()to sort a list in place. It returnsNone, so do not assign its return value to the list.
Solution: NextGen CTO Student Enrollment Manager
The solution uses a set to eliminate duplicate names and converts the result back into a list so that a new student can be appended and the names sorted.
raw_students = [
"Aman", "Priya", "Rahul", "Aman", "Neha",
"Priya", "Ishan", "Rahul", "Avesh"
]
# Remove duplicate student names
unique_students = set(raw_students)
# Convert the set back into a list
students = list(unique_students)
# Add a newly enrolled student
students.append("Payal")
# Sort students alphabetically
students.sort()
# Display the final student list
print(students)
Solution explanation
- Lines 1–4: Initialize the raw list containing nine entries, including repeated names.
- Line 7: Convert the list into a set, leaving six unique names. Sets do not guarantee ordering.
- Line 10: Convert the set back into a list to enable list operations.
- Line 13: Append Payal to the list, increasing its size to seven.
- Line 16: Sort the list in place in alphabetical order.
- Line 19: Print the final sorted list.
Test cases
This exercise assumes the new student is not already enrolled. In production, deduplicate enrollment records by unique student IDs or email addresses, not names.