you need to reset l2 for every row
import csv from statistics import mean with open ('grades.csv', 'r') as input_file: reader=csv.reader(input_file) data = [] for row in reader: l2 = [] name = row[0] grades=[int(num) for num in row[1:]] # alternatively use: grades = map(int, row[1:]) l2.append(name) l2.append(float(mean(grades))) data.append(l2) # or just as a single line # data.append([row[0], float(mean(grades))]) with open('grades.csv', 'w', newline='') as output_file_name: writer = csv.writer(output_file_name) writer.writerows(data)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs