Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why don't the lists get inserted correctly into my dictionary?

a should be [-1,-1,-1], b should be [-1,-1,0]z should be [1,1,0] and lastly should be [1,1,1]. When print(i, "is: ", temp) is run through every loop the correct answer gets shown, but when I print the dictionary after the loop all the values are [2,-1,-1].

alfabet = "abcdefghijklmnopqrstuvwxyz "

alfabetKod = {}


temp = [-1,-1,-1]

for i in alfabet:
    print(i, "is: ", temp)
    alfabetKod.setdefault(i, temp)
    

    if temp[2] == 1:
        temp[2] = -1
        if temp[1] == 1:
            temp[1] = -1
            temp[0] += 1
        else:
            temp[1] += 1
    else:
        temp[2] += 1
    
        
    
print(alfabetKod)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You’re writing only a reference to the list into your dict, not the current values. Try changing alfabetKod.setdefault(i, temp) to alfabetKod.setdefault(i, temp.copy()) to write the values instead of a reference.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading