I'm writing a function with 2 params `(data, keys)` that will take `schools_list` as the first param (see below) and a tuple `groupby_keys` as the second param:

```
groupby_keys = ('region', 'state')
schools_list = [
    {'region': 'northeast', 'state': 'MA', 'school': 'Brandeis', 'stu_pop': 5800},
    {'region': 'south', 'state': 'GA', 'school': 'Gatech', 'stu_pop': 36489},
    {'region': 'westcoast', 'state': 'CA', 'school': 'Stanford', 'stu_pop': 17249},
    {'region': 'northeast', 'state': 'MA', 'school': 'Olin', 'stu_pop': 390},
    {'region': 'south', 'state': 'TX', 'school': 'UT Austin', 'stu_pop': 51090},
    {'region': 'northeast', 'state': 'CT', 'school': 'Yale', 'stu_pop': 13609},
    {'region': 'northeast', 'state': 'CT', 'school': 'Trinity College', 'stu_pop': 2198},
    {'region': 'westcoast', 'state': 'OR', 'school': 'Reed', 'stu_pop': 1470},
    {'region': 'westcoast', 'state': 'CA', 'school': 'Harvey Mudd', 'stu_pop': 895},
    {'region': 'westcoast', 'state': 'WA', 'school': 'UW', 'stu_pop': 47571},
    {'region': 'south', 'state': 'TX', 'school': 'TCU', 'stu_pop': 11024},
    {'region': 'northeast', 'state': 'MA', 'school': 'Tufts', 'stu_pop': 11878},
    {'region': 'south', 'state': 'TX', 'school': 'SMU', 'stu_pop': 12373},
    {'region': 'westcoast', 'state': 'OR', 'school': 'Lewis & Clark', 'stu_pop': 3390}
]

```

This function should group by (**without using numpy and pandas**) the dicts in the list in the 1st param by keys specified by the tuple in the 2nd param and return output like this:

```
{
('northeast','MA'):
   [{'region':'northeast', 'state':'MA', 'school':'Brandeis', 'stu_pop':5800},
    {'region':'northeast', 'state':'MA', 'school':'Tufts', 'stu_pop':11878}],
('northeast','CT'): 
   [{'region':'northeast', 'state':'CT', 'school':'Yale', 'stu_pop':13609}, 
    {'region':'northeast', 'state':'CT', 'school':'Trinity College', 'stu_pop':2198}],
...
}

```

Here is my code:

```
def group_by_field(source, fields):
    data = source 
    value_sets = []
    #create a dict with unique tuples-keys from the data
    for datum in data:
        temp = []
        for field in fields:
            if datum[field] not in temp:
                temp.append(datum[field])
        if temp not in value_sets:
            value_sets.append(tuple(temp))   
    groups = dict.fromkeys(value_sets, [])
    #the check function check whethers a dict has values specified by the tuple 
    def check(dic, fields, tup):
        sum_check = len(fields)
        for field, val in zip(fields,tup):
            if dic[field] == val:
                sum_check = sum_check - 1
        if sum_check == 0:
            return True
        return False
    #append the correct dict to the correct tuple-key
    for value_set in value_sets:
        for datum in data:
            if check(datum, fields, value_set):
                groups[value_set].append(datum)
                data.remove(datum) #so that the 1st for-loop doesn't have to loop through this value again
    return groups

```

The issue is my code works if the list doesn't have a lot of elements, but when the number of elements is a few thousands, it runs really slowly. How should I optimize this?

Many thanks!