Python Forum
In this dictionary all the values end up the same. How? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: In this dictionary all the values end up the same. How? (/thread-30631.html)



In this dictionary all the values end up the same. How? - Pedroski55 - Oct-28-2020

What I'm doing is making a big excel with all the students homework answers in each row. The first 2 columns are name and student number.

My goal is to make a MySQL table with all the students' answers, so they can click a button and see their answers and the correct answers. Eventually, I will do everything on the webhost in MySQL and stop downloading files.

A typical file is a text file with a name composed of:

Quote:studentnumber_COURSEweek_microtime epochtime like this: 1925010101_19BEwW3_0.74946700 1600771500

The times are so each submission will have a different name, in case a student submits more than 1 time.
The content is like this, just longer:

Quote:Studentnr = 1925010101
B
B
C
A
These are written to my webpage by PHP, I download them and mark them.

Yesterday, I did this below. Can anyone see anything wrong with it?

When I checked the dictionary studentAnswers, the keys were all different, but the values were all the same.

The value was always a list of the answers in the last file in the list of files weekXfiles. The student number was 1925010310. FOR EVERY KEY!

I can't understand what is wrong, apart from thinking there is a bug in my Python installation, or in Idle.

weekXfiles is a list of all files from 1 particular week, like 19BEwW3

studentAnswers = {}
studentAnswersList = []

for hw in weekXfiles:
    studentAnswersList.clear()    
    data = open(pathHW + hw, 'r')
    dataList = data.readlines()    
    data.close()
    name = hw[21:]    
    for i in range(0, len(dataList)):
        studentAnswer = dataList[i].replace('\n', '')
        studentAnswersList.append(studentAnswer)
    studentAnswers[name] = studentAnswersList    
I do the same kind of thing often, for example, for marking: use the student number as key and the score as value, make a dictionary, then read it into excel. Never had this problem.

In the end I avoided using a dictionary and just made a list and wrote it directly to excel. That worked.


RE: In this dictionary all the values end up the same. How? - bowlofred - Oct-28-2020

Each time through the loop you're reusing the same list. When you .clear() the list, you're clearing the data from all the students (because they share the same object).

You want to use a unique object for each student. Instead of creating outside the loop and reusing it, create it new each time.

studentAnswers = {}
 
for hw in weekXfiles:
    studentAnswersList = []  # New object for this student.
    ...  



RE: In this dictionary all the values end up the same. How? - Pedroski55 - Oct-29-2020

Thank you, you are right, I just tried it!

I thought studentAnswersList.clear() did the same job as studentAnswersList = []

I don't understand it, but I am the wiser, thank you!