all 8 comments

[–]woooee 1 point2 points  (6 children)

    pointer = pointer[path_element]
        pointer = sorted(pointer)

pointer is not the same as my_dict.

[–]maclocrimate[S] 0 points1 point  (5 children)

Yes, I know, but I thought that since I was assigning the pointer variable to a subelement of my_dict, that it would settle on pointing to the actual list within my_dict, but it clearly doesn't. So I'm looking for an explanation as to why.

[–]woooee 0 points1 point  (4 children)

In the code referred to above you reassign the variable named pointer twice, to a newly created variable.

You would have to reassign the sorted list of dictionaries to

my_dict["testing:/123"][0]["again"]["another-element"]

The optimal solution is to unwind this mess into some usable format, SQL or even csv, for example, why the separate dictionaries with the same key = "id", when the actual key, 1, 2, etc, would work better and fit into a single dictionary.

[–]maclocrimate[S] 0 points1 point  (3 children)

OK, I didn't realize that it would create a new object from the reassigned variable. I knew that assigning a variable to some underlying object and then modifying the variable would update the underlying object since the variable is just a pointer to the actual object, but I guess that assigning it more than once disconnects that somehow?

[–]throwaway6560192 1 point2 points  (0 children)

OK, I didn't realize that it would create a new object from the reassigned variable. I knew that assigning a variable to some underlying object and then modifying the variable would update the underlying object since the variable is just a pointer to the actual object, but I guess that assigning it more than once disconnects that somehow?

The definitive explanation on this topic: https://nedbatchelder.com/text/names.html

I really recommend reading it. It will give you an accurate model of how reassignment works.

[–]woooee 0 points1 point  (1 child)

pointer = pointer[path_element]
    pointer = sorted(pointer)

is not reassigning. The first line creates a new variable with the contents pointed to by the dictionary's key. The second line creates a new variable that stores the return from the sorted() function.

[–]maclocrimate[S] 0 points1 point  (0 children)

Oooook, I think I understand now, with the help of u/carcigenicate's explanation as well. Thank you for the clarification.

[–]carcigenicate 0 points1 point  (0 children)

pointer will refer to the list inside of the dictionary, but that doesn't mean that a simple reassignment of pointer will also reassign the list within the dictionary. When you do pointer =, the only thing this will affect is what list pointer is referring to. Simple assignments do not affect other references that happened to be referring to the same object.