Modify a dictionary under a new name modifies the original dictionary

x=Dict(
            row=> DataFrame(
                            y=1)
)

z=x

push!(z, y=2)

x

gives me

1,2

How do I get it to only give me 1?

This never copies x, it always makes z refer to the exact same object as x.

If you want to copy, explicitly call copy(x) (or deepcopy, if necessary and you want completely distinct objects in the case of containers).

3 Likes
z=copy(x)

gives the same result

copy is a shallow copy, i.e. it’s not copied recursively. That’s what deepcopy is for, as I’ve mentioned above. Your x is a Dict, so regular copy creates a distinct Dict instance, but the DataFrame inside is still the same object.

I don’t know what row is, so I can only guess at what’s going wrong. If it still doesn’t work, please provide a MWE to make it easier to debug your problem.

2 Likes

row just means a new row. I thought this was an MWE.

deepcopy(x)

appears to do the same thing, unless there is a different command, or a different way to execute it?

As the code is written, I don’t know where row comes from - is it from a package? Without that, I can’t run that code.

That definitely shouldn’t be the case. Are you perhaps in an old session with existing bindings? Please show your full code so I can reproduce it on my machine.

2 Likes

Yes, it was old bindings, I refreshed, and now it works.