1

I'm currently studying Python, and I've encountered something I don't understand. What I want to do is:

  1. I have variables a, b, c, and I want to assign a dictionary k={'sd':'asd', 'we':'qwe'}, so I typed like this:
a=k
b=k
c=k
  1. And if I edit a like, a['are']='friend', I found out that b and c also has are key even though I only edit a.

I don't understand cuz I a, b, and c are all different variable, and how they are "linked" each other..? And what should I do for what I supposed to do?

1

1 Answer 1

2

There's only one dictionary, referenced by three variables, so making changes to it via any one variable reflects everywhere. You might want to make copies of the dictionary.

a = k.copy()
b = k.copy()
c = k.copy()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.