Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:12:33+00:00 2026-06-17T05:12:33+00:00

I noticed something odd while helping a collegue with his Python 2.7 script. He

  • 0

I noticed something odd while helping a collegue with his Python 2.7 script. He has a dictionary with lists as the dictionary values. He was assigning the dictionary values to a new variable and then performing some edits to the list. The odd part that got me was the list changes in the new variable were also being reflected in the dictionary. I repeated a simple example in the shell and I posted it below. I also tried the same thing but with a string as the dictionary value, but to no effect. Is there something I am missing here or is this some sort of bug? Thanks.

>>> dict1 = {}
>>> dict1['foo'] = [1,2,3]
>>> print dict1     
 {'foo': [1, 2, 3]}    
>>> bar = dict1['foo']
>>> bar.append(4)
>>> print dict1    
 {'foo': [1, 2, 3, 4]}

In the example above I would have expected the 4 to be appended to bar and the value of ‘foo’ to remain [1,2,3].

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:12:34+00:00Added an answer on June 17, 2026 at 5:12 am

    You don’t get a copy of the list, you get a reference to the list itself. Since lists are mutable, the behaviour is entirely expected.

    Compare this to using a second variable pointing to a list:

    >>> a = [1, 2, 3]
    >>> b = a
    >>> b.append(4)
    >>> a
    [1, 2, 3, 4]
    

    If you wanted to have an independent list instead, make a copy:

    >>> c = list(a)
    >>> c.append(5)
    >>> c
    [1, 2, 3, 4, 5]
    >>> a
    [1, 2, 3, 4]
    

    Another way to make a copy is to use the full-list slice:

    >>> c = a[:]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While coding today, I noticed something odd with timespans and formatting strings. I was
Something odd I noticed... in Powershell, if I attempt the following: $uri = new-object
While processing multiple gigabyte files I noticed something odd: it seems that reading from
While testing some HTTP server code, I noticed something odd: if I return Content-Type:
I was looking at the www.google.com in Firebug and noticed something odd: The Google
I noticed something a little odd with the CoffeeScript compilier and was wondering if
I was coding today and noticed something. If I open a new interpreter session
While working on something non-development related, I noticed something interested while on answers.com .
We've noticed something odd with our Coldfusion 8 client variables DB. We have many
I've noticed something odd about using the bitwise XOR operator on byte s in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.