Start learning free

LESSON 182 OF 287 · LOOPING OVER LIST INDICES

Employee of the Year V4 in Python: Finding the Best with a for Loop

Practice Read the full explanation

A whole team is competing. Use a for loop over the lists to find the most productive employee, then print their 15% bonus.

You start with:

names = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"]
productivities = [83, 133, 97, 150, 120]
salaries = [135000, 335000, 210000, 250000, 180000]

Build:

Alice Chen
Bonus: $37500

Rules:

  • Index i in all three lists refers to the same employee.
  • The winner is whichever employee has the highest productivity. If two employees share the highest productivity, the one listed first wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Start with best_index = 0 and loop over the other indices with for i in range(1, len(names)). Inside the loop, update the best index only when productivities[i] > productivities[best_index]: a strict comparison keeps the first employee on a tie.

Use integer division // so the bonus stays a whole number: bonus = salaries[best_index] * 15 // 100.

Joining text and a number needs str(): print("Bonus: $" + str(bonus))

names = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"]
productivities = [83, 133, 97, 150, 120]
salaries = [135000, 335000, 210000, 250000, 180000]

best_index = 0
for i in range(1, len(names)):
    if productivities[i] > productivities[best_index]:
        best_index = i
bonus = salaries[best_index] * 15 // 100
print(names[best_index])
print("Bonus: $" + str(bonus))

STEP 1 OF 6
    Ctrl + Enter to check

    PYTHON · LOOPING OVER LIST INDICES

    Employee Of The Year V4 in Python, explained

    Problem overview

    Version 4 scales the program to any number of employees. The data is now in three parallel lists: names, productivities and salaries. Index i in each list describes the same person. The employee with the highest productivity wins, and on a tie the one listed first wins. The program prints the winner's name and then Bonus: $ followed by 15% of their salary, rounded down.

    With five employees whose scores are 83, 133, 97, 150 and 120, the highest is 150, which belongs to Alice Chen at index 3. Her salary is 250000, so the bonus is 37500:

    Alice Chen
    Bonus: $37500

    Because the data is in lists, one loop handles every employee, whatever the team size. The program needs no separate variables for each person.

    Approach: remember the index of the best so far

    This is the classic "find the maximum" scan. Keep a variable, here best_index, holding the position of the most productive employee seen so far. Start by assuming employee 0 is the best. Then visit every other employee once, and whenever one has a strictly higher productivity than the current best, update best_index.

    Storing the position instead of the productivity is the key idea. Once the loop is done, one number gives access to everything about the winner: names[best_index] and salaries[best_index] are the right name and salary because the lists are parallel. Had the loop stored only the highest score, it would have no way to find the matching name.

    The comparison is strict (>). A later employee replaces the current best only by being strictly better, so when two share the top score the earlier one stays in front, as the rules require.

    Step by step

    1. Create best_index and set it to 0. Until somebody beats employee 0, employee 0 is the best candidate.
    2. Loop over the other positions. for i in range(1, len(names)) makes i take the values 1 up to the last index. It starts at 1 because position 0 is already the current best, and comparing it with itself would change nothing.
    3. Compare inside the loop. If productivities[i] is strictly greater than productivities[best_index], assign best_index = i.
    4. Compute the bonus after the loop: salaries[best_index] * 15 // 100. The // operator divides and rounds down, so the amount stays a whole number.
    5. Print the name with names[best_index].
    6. Print the bonus line, converting the number with str() before joining it to "Bonus: $".

    The solution in Python

    names = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"]
    productivities = [83, 133, 97, 150, 120]
    salaries = [135000, 335000, 210000, 250000, 180000]
    
    best_index = 0
    for i in range(1, len(names)):
        if productivities[i] > productivities[best_index]:
            best_index = i
    bonus = salaries[best_index] * 15 // 100
    print(names[best_index])
    print("Bonus: $" + str(bonus))
    

    Some details worth knowing:

    • range(1, len(names)). The end value is excluded, so with five employees the loop sees 1, 2, 3 and 4. List indexes start at 0, so these are the second to the fifth employee.
    • The loop variable stays visible. After the loop, i still holds the last index visited (4 here), which is not the winner. Always use best_index after the loop.
    • Everything after the loop is unindented. The bonus and the prints run once, after the scan is complete. Indenting them under the loop would compute and print on every iteration.
    • Indexes pair the lists. The lists are separate objects, and nothing in Python links them. The program is correct only because the data was written in the same order in all three.

    Tracing the example

    The scan starts with best_index = 0 (score 83). At i = 1, 133 is greater than 83, so best_index becomes 1. At i = 2, 97 is not greater than 133, so nothing changes. At i = 3, 150 is greater than 133, and best_index becomes 3. At i = 4, 120 is not greater than 150. The loop ends with best_index equal to 3, so the program reports names[3], "Alice Chen", with a bonus of 250000 * 15 // 100 = 37500.

    Edge cases and common mistakes

    • Ties. With scores [100, 100], the second is not strictly greater, so index 0 wins. Using >= would let the later employee take the title.
    • The best employee is first. With scores [150, 120, 90, 80, 70] the condition never fires and best_index stays 0, which is correct.
    • One employee. range(1, 1) is empty, so the loop body never runs, and employee 0 wins by default.
    • Starting at the wrong place. range(0, ...) still works, because the first comparison is the employee against itself, but it does a pointless step. range(2, ...) would skip employee 1 and could miss the winner.
    • Comparing against i - 1. Looking at the neighbour finds a local peak, not the overall best. Compare with best_index.
    • Printing names[i] after the loop. That prints the last employee, not the winner.

    Testing

    • productivities = [150, 120, 90, 80, 70] picks John Doe: Bonus: $20250.
    • Scores of [90, 150, 150, 80, 70] pick Mary Jane, the first of the two 150s.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free