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

*SOLVED* Python: Appending to pre-populated list within a dictionary of lists alters all lists within dictionary

Thanks @StevenRumbalski for correcting the code I posted and figuring out the problem. I was using a pre-defined list to populate the lists within the dictionary, so when I appended to any list within the dictionary, the referenced dictionary was updated, causing all of the lists in the dictionary to change at once.

I have encountered a puzzling error when I’m attempting to sort some values into a dictionary of lists using string prefixes. Here is the basic code I am running:

values = ["a_one", "a_two", "b_three", "b_four", "c_five", "c_six"]
value_groups = {"a": [],
                "b": [],
                "c": []}

for value in values:
    for prefix in value_groups.keys():
        if value.startswith(prefix):
            value_groups[prefix].append(value)
            break

The desired, and actual output of this code is:

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

value_groups = {"a": ["a_one", "a_two"],
                "b": ["b_three", "b_four"],
                "c": ["c_five", "c_six"]}

So the code above works as-is.

My problem arises when I initialize the lists within value_groups with one or more strings. The code below is updated with my mistake. In this case, every value in values gets appended to every list within value_groups. Non-functional code below:

initial_list = ["x", "y", "z"]
values = ["a_one", "a_two", "b_three", "b_four", "c_five", "c_six"]
# ---START OF EDITED CODE---
value_groups = {"a": initial_list,
                "b": initial_list,
                "c": initial_list}
# ---END OF EDITED CODE---

for value in values:
    for prefix in value_groups.keys():
        if value.startswith(prefix):
            value_groups[prefix].append(value)
            break

The desired output of this code would be:

value_groups = {"a": ["x", "y", "z", "a_one", "a_two"],
                "b": ["x", "y", "z", "b_three", "b_four"],
                "c": ["x", "y", "z", "c_five", "c_six"]}

But instead, I am getting three identical lists within value_groups:

value_groups = {"a": ["x", "y", "z", "a_one", "a_two",
                      "b_three", "b_four", "c_five", "c_six"], 
                "b": ["x", "y", "z", "a_one", "a_two",
                      "b_three", "b_four", "c_five", "c_six"], 
                "c": ["x", "y", "z", "a_one", "a_two",
                      "b_three", "b_four", "c_five", "c_six"]}

My intuition (and preliminary stackoverflow research) is that there is some sort of memory referencing mechanism that I am running afoul of, but I am not quite sure what it is. Anyone out there able to explain it to me? (if that is, indeed, the problem…)

>Solution :

value_groups = {"a": ["x", "y", "z"],
                "b": ["x", "y", "z"],
                "c": ["x", "y", "z"]}

I’m pretty certain that’s not how you’re defining value_groups. What you’ve probably done is something like this (where some_list was previously defined):

some_list = ["x", "y", "z"]
value_groups = {"a": some_list, 
                "b": some_list, 
                "c": some_list}`   

If that’s the case, every key in the dict refers to the same list. A fix would be to make copies of some_list with an empty slice (some_list[:] or some_list.copy()):

some_list = ["x", "y", "z"]
value_groups = {"a": some_list[:], 
                "b": some_list[:], 
                "c": some_list[:]}
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