1

Python Object Changing For No Apparent Reason

I create a dict of various objects... Everytime I run the script (I have checked that the file updating), the second index of the first few objects in the dict are changed. I am not changing them anywhere in my code. I threw the dict and print into a separate python file with nothing else and I still got the same results.

checklist = {
        "pine logs": ['logs', '2', '4'],
        "travertine": ['rocks', '2', '4'],
        "iron ore": ['ores', '2', '4'],
        "medium hide": ['hides', '2', '4'],
        "hemp": ['fibers', '2', '4'],
        "pine logs": ['logs', '3', '4'],
        "travertine": ['rocks', '3', '4'],
        "iron ore": ['ores', '3', '4'],
        "medium hide": ['hides', '3', '4'],
        "hemp": ['fibers', '3', '4'],
        "cedar logs": ['logs', '1', '5'],
        "granite": ['rocks', '1', '5'],
        "titanium ore": ['ores', '1', '5'],
        "heavy hide": ['hides', '1', '5'],
        "skyflower": ['fibers', '1', '5'],
        "cedar logs": ['logs', '2', '5'],
        "granite": ['rocks', '2', '5'],
        "titanium ore": ['ores', '2', '5'],
        "heavy hide": ['hides', '2', '5'],
        "skyflower": ['fibers', '2', '5'],
        "bloodoak logs": ['logs', '1', '6'],
        "slate": ['rocks', '1', '6'],
        "runite ore": ['ores', '1', '6'],
        "robust hide": ['hides', '1', '6'],
        "redleaf cotton": ['fibers', '1', '6'],
        "bloodoak logs": ['logs', '2', '6'],
        "slate": ['rocks', '2', '6'],
        "runite ore": ['ores', '2', '6'],
        "robust hide": ['hides', '2', '6'],
        "redleaf cotton": ['fibers', '2', '6'],
        "ashenbark logs": ['logs', '1', '7'],
        "basalt": ['rocks', '1', '7'],
        "meteorite ore": ['ores', '1', '7'],
        "thick hide": ['hides', '1', '7'],
        "sunflax": ['fibers', '1', '7'],
        "ashenbark logs": ['logs', '2', '7'],
        "basalt": ['rocks', '2', '7'],
        "meteorite ore": ['ores', '2', '7'],
        "thick hide": ['hides', '2', '7'],
        "sunflax": ['fibers', '2', '7']
    }
    print(checklist)

output:

{'pine logs': ['logs', '3', '4'], 'travertine': ['rocks', '3', '4'], 'iron ore': ['ores', '3', '4'], 'medium hide': ['hides', '3', '4'], 'hemp': ['fibers', '3', '4'], 'cedar logs': ['logs', '2', '5'], 'granite': ['rocks', '2', '5'], 'titanium ore': ['ores', '2', '5'], 'heavy hide': ['hides', '2', '5'], 'skyflower': ['fibers', '2', '5'], 'bloodoak logs': ['logs', '2', '6'], 'slate': ['rocks', '2', '6'], 'runite ore': ['ores', '2', '6'], 'robust hide': ['hides', '2', '6'], 'redleaf cotton': ['fibers', '2', '6'], 'ashenbark logs': ['logs', '2', '7'], 'basalt': ['rocks', '2', '7'], 'meteorite ore': ['ores', '2', '7'], 'thick hide': ['hides', '2', '7'], 'sunflax': ['fibers', '2', '7']}

Why is index 1 of the first few objects in the dict changing? I even tried changing the '2' to 'two' but it still ends up as '2'.

Submitted September 19th 2021 by Admin

Answers
0

You have duplicate keys in the dict defintion. In this case, the later values will override the earlier ones. For example:

checklist = { "pine logs": ['logs', '2', '4'], ... "pine logs": ['logs', '3', '4'], ...
}

This is equivalent to:

checklist = { ... "pine logs": ['logs', '3', '4'], ...
}

If you want to have multiple instances of the same key, you can either keep a list of value lists for each key, or you can switch to a list representation.

Admin | 1 month ago


0

you should use for loop and select what results you wan't

Admin | 1 month ago


0

Your dictionary checklist violates the unique key constrain (duplicate pine logs):

It is best to think of a dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary).1

Admin | 1 month ago



Relevant Questions