I am new to Python and trying to solve a problem but cant figure it out

I have a list
group_list = [2,3,2,1]
Each element represents the number of people in a group.
And the group number is the index of each element
Group # Number of people

0 2
1 3
2 2
3 1

I want to now assign each individual its group number so i have a data structure like this
where a letter represents each person’s name and it is unique
Person Group #

A 0
B 0
C 1
D 1
E 1
F 2
G 2
H 1

Please how do i do this?

You’ve assigned person “H” to group index 1. I assume that is incorrect.

It looks like you have more people than slots. Given that, I’d rather loop over the slots and pick the people from an iterator. If there were more slots than people, I might do it the other way.

from string import ascii_uppercase

people = iter(ascii_uppercase)

group_counts = [2,3,2,1]

for group_number, group_count in enumerate(group_counts):
    for _ in range(group_count):
        print(f"person {next(people)} -> group index {group_number}")
1 Like