'How can I give an A to first half of this list and a B to the second half?

I'm doing an assignment that tells me to sort this list of students alphabetically and then Print "Name you get an A" for the first half of the list and "Name you get a B" for the second half of the list. I have it sorted alphabetically but have no idea how to split the list

students = ['john','henry', 'abigail', 'zach', 'taylor', 'beatrice']
students = [student.capitalize() for student in students]
sorted_list = sorted(students)
print(sorted_list)


Solution 1:[1]

Extending Matthias' comment, you could do something like this. You first iterate on the first half and print the names, and then you iterate over the other half.

students = ['john','henry', 'abigail', 'zach', 'taylor', 'beatrice']
for a_student in students[:len(students)//2]:
    print(f"{a_student.capitalize()} you get an A")
for b_student in students[len(students)//2:]:
    print(f"{b_student.capitalize()} you get a B")

This outputs:

John you get an A
Henry you get an A
Abigail you get an A
Zach you get a B
Taylor you get a B
Beatrice you get a B

A more compact but unreadable way would be, to craft the assignment text directly in a list and print them.

students = ['john','henry', 'abigail', 'zach', 'taylor', 'beatrice']
assignments = [f"{name.capitalize()} you get an A" if i < len(students)//2 else f"{name.capitalize()} you get a B" for i, name in enumerate(students)]
print('\n'.join(assignments))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ð᠍᠍