Code Logo

Seat Map with enumerate()

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

A classroom assistant has a list of student names in seating order and wants to turn it into a quick lookup table. The first student in the list sits in seat 1, the second student sits in seat 2, and so on.

The result should be a dictionary where each student name is the key and the seat number is the value. For example, if the list is ["Nia", "Budi", "Raka"], the answer should be {"Nia": 1, "Budi": 2, "Raka": 3}.

If the list is empty, the answer should be an empty dictionary. You do not need to sort anything or change the order. The seat numbers come directly from the students' positions in the given list.

This is a simple mapping task where a list position needs to become dictionary data. The interesting part is that Python already has a clean way to loop through values together with their positions.

Example Input & Output

Example 1
Input
students = ["Lia"]
Output
{"Lia": 1}
Explanation

A one-student list assigns seat number 1 to that student.

Example 2
Input
students = []
Output
{}
Explanation

An empty class list produces an empty seat map.

Example 3
Input
students = ["Nia", "Budi", "Raka"]
Output
{"Nia": 1, "Budi": 2, "Raka": 3}
Explanation

Each student is mapped to the seat number matching their list position.

Algorithm Flow

Recommendation Algorithm Flow for Seat Map with enumerate()
Recommendation Algorithm Flow for Seat Map with enumerate()

Solution Approach

This is a natural situation for enumerate() because the problem needs two things at the same time: each student name and its position in the list.

If you loop over the names normally, you get the student values but not their seat numbers. enumerate() fixes that by producing pairs of (index, value) as you iterate. Since classroom seats should start at 1 instead of 0, the cleanest version is to tell Python to start counting from 1.

A direct solution looks like this:

return {name: seat for seat, name in enumerate(students, start=1)}

This dictionary comprehension walks through the list once. For every student name, it stores the current seat number as the value in the resulting dictionary. Because start=1 is used, the numbering already matches the classroom rule without extra arithmetic.

You could also write the same logic with a regular loop if you want the steps to be more visible:

seats = {}
for seat, name in enumerate(students, start=1):
    seats[name] = seat
return seats

Both versions are correct. The main idea is that enumerate() removes the need for a separate counter variable and keeps the position-tracking logic tied directly to the list iteration.

This makes the code short, but it also makes the intent clearer. The problem is about converting ordered list positions into a lookup dictionary, and enumerate() gives you exactly the ordered positions you need. The runtime is O(n), and the output dictionary uses O(n) space for the stored seat assignments.

Best Answers

python - Approach 1
def build_seat_map(students):
    seats = {}
    for seat, name in enumerate(students, start=1):
        seats[name] = seat
    return seats