Search code examples
pythonfor-loopif-statementiterable-unpacking

How can I attach the corresponding index to a list


I have filtered a group of students into three buckets using a for loop and by unpacking a tuple. How can I attach their corresponding student numbers to each score? Thank you.

#create index for 100 students, starting with 1
student_index = list(range(1,101))

#join index with results sheet
student_score_index = list(zip(student_index, results_sheet2))


group_a = []
group_b = []
group_c = []

# Iterate over pairs

for index, pair in enumerate(student_score_index):
    # Unpack pair: index, student_score
    index, score = pair
    # assign student scores into 3 buckets: group_a,group_b,group_c
    if score >= 60:
        group_a.append(score)
    elif score >= 50 and score <=59:
            group_b.append(score)
    else:
        group_c.append(score)

print(group_a)
[61, 67, 63, 62, 62, 62]

The desired result should be something like this for all three groups:

#print corresponding student index number, score    

group_a = [(29,61),(51,67),(63,63),(65,62),(98,62),(99,62)]

Solution

  • If you're comfortable trying out a new library, pandas, I would highly recommend it. They have built-in tools for exactly this kind of work! 🙂

    import pandas as pd
    
    # create index for 100 students, starting with 1
    student_index = list(range(1,101))
    
    # join index with results sheet
    score_index = pd.DataFrame.from_dict({
      "studentID": student_index,
      "scores": results_sheet2,
    })
    
    group_a = score_index.loc[score_index["score"] >= 60]
    group_b = score_index.loc[score_index["score"] <= 59]
    # etc ...
    

    You could actually simplify this further when using pandas, and do the following:

    import pandas as pd
    
    # by default, pandas will generate an index to track each row, 
    # for you. it's accessible by the `.index` property of `score_index`.
    score_index = pd.DataFrame.from_dict({
      "scores": results_sheet2,
    })
    
    group_a = score_index.loc[score_index["score"] >= 60]
    group_b = score_index.loc[score_index["score"] <= 59]
    # etc ...