Code Logo

Present Students List

Published at22 Apr 2026
Python List and Dictionary Easy 0 views
Like0

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

Example 1
Input
attendance = {}
Output
[]
Explanation

An empty attendance record produces an empty list.

Example 2
Input
attendance = {"Ayu": True, "Bima": False, "Cici": True}
Output
["Ayu", "Cici"]
Explanation

Only the students marked True are collected into the result list.

Example 3
Input
attendance = {"Lina": False, "Rudi": False}
Output
[]
Explanation

If nobody is marked present, no names are returned.

Algorithm Flow

Recommendation Algorithm Flow for Present Students List
Recommendation Algorithm Flow for Present Students List

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:

present = []
for name, is_present in attendance.items():
    if is_present:
        present.append(name)
return present

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:

return [name for name, is_present in attendance.items() if is_present]

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

python - Approach 1
def get_present_students(attendance):
    return [name for name, is_present in attendance.items() if is_present]