Search code examples
keyadditionordereddictmutated

Add and change key/value in ordered dictionary without mutating


I am trying to split a value into three different ones with new keys and add them to my dictionary. But I always get the error message: RuntimeError: OrderedDict mutated during iteration

def csv_to_dic(file):
    with open(file, "r") as csvfile:
        # creat object, that can read csv as dictionary (including key)
        reader = csv.DictReader(csvfile)
        # define students as mutable list of dictionary rows
        students = []
        # read each row in file and save into students (load everything into memory)
        for row in reader:
            students.append(row)
        for i in range(len(students)):
            for k, v in students[i].items():
                if k == 'name':
                    string = v.split()
                    students[i].update({'first' : string[0]})
                    students[i].update({'middle' : string[1]})
                    students[i].update({'last' : string[2]})
        return students

I can see, that I am able to change the value of the keys like so:

            if k == 'name':
                string = v.split()
                students[i][k] = string[0]

But I am unable to change the keys or add new ones. What am I doing wrong?


Solution

  • Fixed:

        for row in reader:
            students.append(row)
        for i in range(len(students)):
            name = students[i].get('name')
            name = name.split()
            if len(name) == 3:
                students[i]['first'] = name[0]
                students[i]['middle'] = name[1]
                students[i]['last'] = name[2]
                students[i].pop('name')
            else:
                students[i]['first'] = name[0]
                students[i]['middle'] = None
                students[i]['last'] = name[1]
                students[i].pop('name')
        return students
    

    This part seems to be redundant and causes problems, but I am not sure why:

            for k, v in students[i].items():
                if k == 'name':