Present Students List
A teacher finishes morning roll call and stores the result in a dictionary. Each key is a student name, and each value is either True or False depending on whether that student is present.
Your task is to build a list containing only the names of the students who are marked True. The order in the result should follow the order the names appear in the dictionary, so you are filtering the attendance record rather than sorting it.
For example, if the attendance record is {"Ayu": True, "Bima": False, "Cici": True}, the result should be ["Ayu", "Cici"]. If every value is False, the answer should be an empty list because nobody was marked present.
This problem is small, but it is a useful exercise in reading key-value pairs from a dictionary and collecting selected results into a list. You need to inspect both the student name and the attendance value while you loop.
Example Input & Output
An empty attendance record produces an empty list.
Only the students marked True are collected into the result list.
If nobody is marked present, no names are returned.
Algorithm Flow

Solution Approach
The dictionary already contains all the information you need: each student name is stored as a key, and each attendance decision is stored as the matching value. That means the job is simply to walk through the dictionary and keep the names whose value is True.
A very natural Python way to do that is to iterate over attendance.items(). That gives you both parts together, so each loop step can read a name and an is_present value at the same time.
One clear solution is:
The logic is simple and direct. Start with an empty result list, inspect every entry, and only append the names that pass the condition. Because the loop follows dictionary order, the final list keeps the same order as the original attendance record.
You can also express the same idea with a list comprehension:
That version is shorter, but the underlying reasoning is exactly the same. The problem is not about transforming the names themselves. It is about filtering dictionary entries and collecting just the keys that meet the attendance rule.
The runtime is O(n) because every attendance entry is checked once. The extra space depends on how many students are present, since only those names are stored in the returned list. For an easy dictionary exercise, this is a nice pattern because it connects key-value iteration with list building.
Best Answers
def get_present_students(attendance):
return [name for name, is_present in attendance.items() if is_present]Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
