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 7837355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:36:26+00:00 2026-06-02T14:36:26+00:00

Like below, why does not the final r print [{‘k’: 1}, {‘k’: 2}] ?

  • 0

Like below, why does not the final r print [{'k': 1}, {'k': 2}] ?

l = [1, 2]
d = {}
r = []

for el in l:
    d['k'] = el
    print '> ' + str(d)
    r.append(d)

print r  

OUTPUT:

> {'k': 1}
> {'k': 2}
[{'k': 2}, {'k': 2}]

It is printed as wished before append operation, however after the dict is appended to a list, why does it append the last element repeatedly?

  • 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-02T14:36:27+00:00Added an answer on June 2, 2026 at 2:36 pm

    You’re assigning a new value to the same key when you pass through the loop a second time.

    for el in l:
        d['k'] = el
        print '> ' + str(d)
        r.append(d)
    
    print r  
    

    The first time through the loop, el is 1. d['k'] is set to 1, and then printed. Then d is appended to r.

    The second time through the loop, el is 2. d['k'] — the very same value of the very same key of the very same dictionary — is set to 2. Then the very same dictionary, d, is appended to r.

    Now r is the same as if you had said

    r = [d, d]
    

    And since d['k'] is now 2, both references to d in the list r yield {'k': 2}.

    If you want it to be different, you have to create a new dictionary every time through the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a result from a query like the below, which does not have
I would like to use a spinner. But, this code below does not display
does a service, which is configured like in the box below, throwing a 404
Does anyone know how to implement an action badge, like the ones seen below
I created a simple table like below. CREATE TABLE CUSTOMERS ( CUST_ID INTEGER NOT
This simple program below does not build for some reason. It says undefined reference
Like below,I need to send name and psw value to server side, and get
I have a line like below. fullname = (this is a test name); I
i have an array like below int[] array = new array[n];// n may be
I have pattern like below hi hello hallo greetings salutations no more hello for

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.