First of all: don't use Python2, it's end of life. The telltale sign that you're using it is the lack of parentheses to the print function.
Since Python has a built-in library for reading CSV files, it would make sense to use it. The parsing would be more elegant and straightforward.
Your function get_all_courses
expects exactly two arguments. It would be more flexible to use only one argument, but call the function as many times as necessary. Or use a list of files as parameter.
As for the aggregation, I think your logic is fairly to the point.
The next step is to write a function that reads from a single file (given as parameter) and returns a dict-like structure. Which you did. But I'd rather return the file data with as little transformation as possible and instead delegate the consolidation to another function to better separate concerns.
My implementation follows below. I wouldn't call it superior, because there are actually more steps involved. It is more for demonstration purposes of the features available in Python.
For more flexibility I have added a delimiter parameter in the csv reader routine, in case you have to parse files that are not comma-delimited.
My example returns a list of dictionaries from each file, like:
[{'Name': 'Susan', 'Physics': '6.0', 'Chemistry': '5.5'}, {'Name': 'James', 'Physics': '4.7', 'Chemistry': '8.5'}, {'Name': 'Linda', 'Physics': '4.5', 'Chemistry': '6.3'}, {'Name': 'Mathew', 'Physics': '7.7', 'Chemistry': '5.4'}]
The idea is to merge those lists into one single list, hence the extend method.
Finally, we need to apply some form of "group by" to the results and calculate averages. We want to group by name. For this we use the itertools.groupby function. There is one caveat though: the dataset must be sorted by whatever column(s) shall be used as the grouping key.
Therefore we sort our list of dict by name beforehand.
We end up with something like this:
James : [{'Name': 'James', 'Physics': '4.7', 'Chemistry': '8.5'}, {'Name': 'James', 'History': '6.3', 'Math': '7.5', 'Arts': '5.9'}]
Linda : [{'Name': 'Linda', 'Physics': '4.5', 'Chemistry': '6.3'}, {'Name': 'Linda', 'History': '8.3', 'Math': '7.4', 'Arts': '6.9'}]
Mathew : [{'Name': 'Mathew', 'Physics': '7.7', 'Chemistry': '5.4'}, {'Name': 'Mathew', 'History': '5.8', 'Math': '8.9', 'Arts': '4.9'}]
Susan : [{'Name': 'Susan', 'Physics': '6.0', 'Chemistry': '5.5'}, {'Name': 'Susan', 'History': '8.8', 'Math': '9.0', 'Arts': '5.9'}]
At this point the rest is easy, we simply read the dict values for each student and compute averages while ignoring the 'Name' key. (Actually I am not completely satisfied with this attempt, and there are countless approaches possible).
import csv
from itertools import groupby
from operator import itemgetter
from functools import reduce
from statistics import mean
def get_data_from_file(filepath: str, delimiter: str = ",") -> list:
"""Read a CSV file and return a list of dict eg:
Return a list of dict eg:
[
{'Name': 'Susan', 'Physics': '6.0', 'Chemistry': '5.5'},
...
]
Default field delimiter is a comma
"""
with open(filepath) as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=delimiter)
return list(csv_reader)
all_courses = []
all_courses.extend(get_data_from_file(filepath='science_courses.csv'))
all_courses.extend(get_data_from_file(filepath='other_courses.csv'))
# the dataset must be sorted before we apply groupby
all_courses = sorted(all_courses, key=itemgetter('Name'))
# group by Name
for key, group in groupby(all_courses, lambda x: x["Name"]):
# get all points for this student
student_points = list(group)
# merge the dicts - source: https://stackoverflow.com/a/16048368
student_points = reduce(lambda a, b: dict(a, **b), student_points)
# drop the "Name" key
del student_points["Name"]
# compute average of points for student
points_average = mean([float(value) for value in student_points.values()])
print(f"Student: {key}, average: {points_average}")
The bottom line is that parsing the CSV file can be streamlined with the built-in library. Python provides a number of functions/libs for aggregating results, such as groupby, or statistics.
PS: for more complex usage Pandas is very popular among python programmers. In fact the same task could have been achieved with Pandas instead, and more likely in a more efficient way.
Useful links