DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

How to Distribute Employee Shifts Automatically in Python

As businesses continue to grow, managing employee shifts becomes more challenging. Whether you're scheduling shifts for a team of workers or organizing a volunteer group, automation can help streamline the process. In this post, we'll look at how you can use Python to automatically assign employees to shifts with just a few lines of code.

Problem Overview

Imagine you have a list of employees and a set of shifts that need to be assigned. It can be tedious to manually match each employee with a shift, especially when the number of employees and shifts grows. Luckily, Python can help make this task easier by distributing employees to shifts in a fair and automated manner.

In this example, we will create a Python script that distributes employees to shifts in a round-robin fashion. This means the first employee gets the first shift, the second employee gets the second shift, and so on. When we run out of employees, we start again at the first one.

The Python Code

Let's break down the code that accomplishes this task:

# List of employees
employees = ["Michael", "John", "Linda", "Jessica", "Charles", "Jack", "Samanta"]

# List of shifts
shifts = [
    "Sun 00:00 - Sun 10:00",
    "Sun 10:00 - Sun 16:00",
    "Sun 16:00 - Sun 23:59",
    "Mon 00:00 - Mon 10:30",
    "Mon 10:30 - Mon 15:59",
    "Mon 16:00 - Mon 20:59",
    "Mon 21:00 - Tue 11:59",
    "Tue 12:00 - Tue 20:59",
    "Tue 21:00 - Tue 23:59",
    "Wed 00:00 - Wed 10:59",
    "Wed 11:00 - Wed 12:59",
    "Wed 13:00 - Sat 23:59"
]

# Distribute employees to shifts
shift_assignment = {}

for i, shift in enumerate(shifts):
    employee = employees[i % len(employees)]  # Use modulus to loop over the employees
    shift_assignment[shift] = employee

# Print out the shift assignments
for shift, employee in shift_assignment.items():
    print(f"{shift}: {employee}")
Enter fullscreen mode Exit fullscreen mode

Code Explanation

  1. List of Employees:

    We begin by defining a list of employees who need to be assigned to shifts. In this case, we have seven employees: Michael, John, Linda, Jessica, Charles, Jack, and Samanta.

  2. List of Shifts:

    Next, we define the shifts in a list. These shifts span from Sunday to Wednesday, with various time frames. You can add as many shifts as needed depending on your requirements.

  3. Distributing Employees to Shifts:

    We then create an empty dictionary called shift_assignment to store the employee names and their corresponding shifts. The core of this logic lies in the for loop:

   for i, shift in enumerate(shifts):
       employee = employees[i % len(employees)]  # Use modulus to loop over the employees
       shift_assignment[shift] = employee
Enter fullscreen mode Exit fullscreen mode
  • We loop over each shift using enumerate(). This gives us both the index (i) and the shift itself.
  • The line employee = employees[i % len(employees)] uses the modulus operator (%) to cycle through the employees. This ensures that once we reach the end of the list of employees, we start again at the first employee. For example, when i = 7, it loops back to employee[0].
  • This loop assigns an employee to each shift in a round-robin manner.
  1. Output the Shift Assignments: Finally, we loop through the shift_assignment dictionary and print the shift along with the employee assigned to it:
   for shift, employee in shift_assignment.items():
       print(f"{shift}: {employee}")
Enter fullscreen mode Exit fullscreen mode

This produces output like this:

   Sun 00:00 - Sun 10:00: Michael
   Sun 10:00 - Sun 16:00: John
   Sun 16:00 - Sun 23:59: Linda
   Mon 00:00 - Mon 10:30: Jessica
   Mon 10:30 - Mon 15:59: Charles
   Mon 16:00 - Mon 20:59: Jack
   Mon 21:00 - Tue 11:59: Samanta
   Tue 12:00 - Tue 20:59: Michael
   Tue 21:00 - Tue 23:59: John
   Wed 00:00 - Wed 10:59: Linda
   Wed 11:00 - Wed 12:59: Jessica
   Wed 13:00 - Sat 23:59: Charles
Enter fullscreen mode Exit fullscreen mode

Advantages of Using This Approach

  1. Simplicity: This approach is incredibly simple and doesn't require complex logic. With just a few lines of code, we can achieve a fair distribution of shifts.

  2. Scalability: If you add more employees or shifts, the code will continue to work without modification. It will automatically cycle through employees based on the number of shifts.

  3. Fairness: The round-robin approach ensures that each employee gets assigned to a shift in turn, preventing any biases or overloads on specific workers.

  4. Customizability: You can easily modify the list of employees and shifts to fit your specific needs. Whether you need a larger team or different shift times, the code is flexible and scalable.

Conclusion

Automating shift assignments can save time and reduce errors in the workplace. Python provides a powerful and easy-to-understand way to handle this task, making it a great solution for businesses of any size. By using lists, loops, and simple logic, you can create a shift scheduling system that distributes shifts fairly and automatically.

If you’re interested in learning more about automating tasks with Python or improving your shift scheduling, be sure to check out additional Python libraries that can help, such as pandas for managing larger datasets or datetime for more complex shift management.

Top comments (0)