title: How can we simplify the list comprehension if we have many types to compare?
body: <p>I'm working with the CSV file to calculate the average score of students base on their race/ethnicity.
  Here is my code to read the file:</p>
  <pre><code># read file
  with open(&quot;StudentsPerformance.csv&quot;) as file:
      data = file.read().split(&quot;\n&quot;)
  
  header = data[0]
  students = data[1:]
  
  # remove last student (empty student)
  students.pop()
  
  # get total number of students
  total_student = len(students)
  
  # split header
  header = header.split(&quot;,&quot;)
  subjects = header[5:]
  
  # split each student in list
  for i in range(len(students)):
      students[i] = students[i].split(&quot;,&quot;)
  </code></pre>
  <p>My column data file after reading it is like this (race is the second column, grades if last three-column):</p>
  <pre><code>['male', 'group D', 'high school', 'free/reduced', 'none', '74', '70', '69']
  ['male', 'group E', 'some high school', 'standard', 'completed', '74', '64', '60']
  ['male', 'group E', &quot;associate's degree&quot;, 'free/reduced', 'none', '64', '56', '52']
  ['female', 'group D', 'high school', 'free/reduced', 'completed', '65', '61', '71']
  ['male', 'group E', &quot;associate's degree&quot;, 'free/reduced', 'completed', '46', '43', '44']
  ['female', 'group C', 'some high school', 'free/reduced', 'none', '48', '56', '51']
  ['male', 'group C', 'some college', 'free/reduced', 'completed', '67', '74', '70']
  ['male', 'group D', 'some college', 'free/reduced', 'none', '62', '57', '62']
  ['male', 'group D', &quot;associate's degree&quot;, 'free/reduced', 'completed', '61', '71', '73']
  ['male', 'group C', &quot;bachelor's degree&quot;, 'free/reduced', 'completed', '70', '75', '74']
  ['male', 'group C', &quot;associate's degree&quot;, 'standard', 'completed', '98', '87', '90']
  ['male', 'group D', 'some college', 'free/reduced', 'none', '70', '63', '58']
  ['male', 'group A', &quot;associate's degree&quot;, 'standard', 'none', '67', '57', '53']
  ...
  </code></pre>
  <p>Then, I found type of race:</p>
  <pre><code>race = []
  race_type = []
  race_number = []
   
  race_type = list(dict.fromkeys(race))
  race_type.sort()
  
  print(race_type)
  
  for i in range(len(race_type)):
      race_number.append(race.count(race_type[i]))
  
  print(race_number)
  </code></pre>
  <p>I have the result:</p>
  <pre><code>['group A', 'group B', 'group C', 'group D', 'group E']
  [89, 190, 319, 262, 140]
  </code></pre>
  <p>So, I have 5 types of race: Group A: 89, Group B: 190, Group C: 319, Group D: 262, Group E: 140.</p>
  <p>Then, I use some code to calculate the average score base on race:</p>
  <pre><code>def compute_average_score(student_group):
      return sum([score for student in student_group for score in list(map(int, student[-3:]))]) / (len(student_group) * 3)
  
  a_group = [x for x in students if x[1] == race_type[0]]
  b_group = [x for x in students if x[1] == race_type[1]]
  c_group = [x for x in students if x[1] == race_type[2]]
  d_group = [x for x in students if x[1] == race_type[3]]
  e_group = [x for x in students if x[1] == race_type[4]]
  
  
  print(round(compute_average_score(a_group), 2))
  print(round(compute_average_score(b_group), 2))
  print(round(compute_average_score(c_group), 2))
  print(round(compute_average_score(d_group), 2))
  print(round(compute_average_score(e_group), 2))
  </code></pre>
  <p>Then, I get the correct result:</p>
  <pre><code>62.99
  65.47
  67.13
  69.18
  72.75
  </code></pre>
  <p>But when I use some code to calculate the average score, I need to input the number to each group: 0, 1, 2, 3, 4, 5.</p>
  <p>Then, I found that the code is not convenient if I had about ten or twenty different race types then it would probably take a lot of time and repeat code a lot. Like I need to input the number to each group: 0, 1, 2, ..., 20.</p>
  <p>So, how can I reduce this computation for this calculation? I thought about using for loop but it doesn't seem very viable.</p>
  <p>Thank you for your help.</p>