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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:13:46+00:00 2026-06-13T22:13:46+00:00

I had this: [{‘name’: ‘Peter’}, {‘name’: ‘Anna’}] And I wanted to make this out

  • 0

I had this:

[{'name': 'Peter'}, {'name': 'Anna'}]

And I wanted to make this out of it:

[{'name': 'Peter Williams'}, {'name': 'Anna Williams'}]

So I did:

>>> li = [{'name': 'Peter'}, {'name': 'Anna'}]
>>> new_li = []
>>> dic = {}
>>> for i in li:
...     dic["name"] = i["name"] + " Williams"
...     new_li.append(dic)

But:

>>> new_li
[{'name': 'Anna Williams'}, {'name': 'Anna Williams'}]

Why?

Could you also show how to best get [{'name': 'Peter Williams'}, {'name': 'Anna Williams'}]?

Edit

The reason why I didn’t understand this behavior is because I assumed that:

>>> dict = {'name':'Peter'}
>>> lis = [dict]
>>> dict['name'] = 'Olaf'

Where

>>> print lis

gives

[{'name': 'Peter'}]

While it actually is

[{'name': 'Olaf'}]
  • 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-13T22:13:47+00:00Added an answer on June 13, 2026 at 10:13 pm

    Because you’re using the same dictionary object in each iteration.

    On the second iteration, you are altering the value that you assigned to the name key in the previous iteration, and your list ends up containing two references to the same object.

    I’d strongly recommend checking out the “Python Tutor” tool (pythontutor.com), which allows you to visualise the execution of some python code, and see what objects are being created in the stack. e.g. Python Tutor with your code

    The correct way to do what you wanted would be:

    li = [{'name': 'Peter'}, {'name': 'Anna'}]
    new_li = []
    for p in li:
        new_p = {'name': p['name'] + ' Williams'}
        new_li.append(new_p)
    

    This way a new dictionary object is created with each iteration.

    A more concise solution:

    li = [{'name': 'Peter'}, {'name': 'Anna'}]
    new_li = [{'name': p['name'] + ' Williams'} for p in li]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had this row: echo <td style='width:11%' id='client_quote_tr' name='client_quote_tr' onclick='changeColorTaskClientQuote(this);'>.$r['task'].</td>; What's the value of
I had this question on an Algorithms test yesterday, and I can't figure out
I had this error : Cannot make a static reference to the non-static method
I had this project with a lot .c files in source directory,then I make
I had this conversation with a colleague, and it turned out to be interesting.
I had this grand scheme to make this cool website with all these bells
I had this string: {\lat\:37.790388261934424,\lng\:-122.46047996826172},{\lat\:37.789608231530124,\lng\:-122.46344112701416} Then thanks to the help of StackOverflowers, I did
[I had this sketched out as a comment elsewhere but decided to create a
I had this working fine while using toggle, but I need to make these
Had this working; at one stage. The problem is the following text is now

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.