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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:11:56+00:00 2026-06-12T13:11:56+00:00

For the needs of the project im iterating over some data and adding needed

  • 0

For the needs of the project im iterating over some data and adding needed values to premade dictionary.

here is striped down example of code which represents my question:

class Parser:
    def __init__(self):
        self.records = [['value1','value2','value3'], 
                        ['value4','value5','value6'],
                        ['value7','value8','value9']]
    def get_parsed(self):
        parsed = []
        dic = {'key1': '',
               'key2': '',
               'key3': '',
               }
        for i in self.records:
            dic['key1'] = i[0]
            dic['key2'] = i[1]
            dic['key3'] = i[2]
            parsed.append(dic)
        return parsed

What i expect to get is list of dicts like this:

[{'key1':'value1','key2':'value2','key3':'value3'},
 {'key1':'value4','key2':'value5','key3':'value6'},
 {'key1':'value7','key2':'value8','key3':'value9'}]

But what im getting is:

[{'key1':'value1','key2':'value2','key3':'value3'},
 {'key1':'value1','key2':'value2','key3':'value3'},
 {'key1':'value1','key2':'value2','key3':'value3'}]

Though if i move dictionary initialization into ‘for’ loop – im getting the desired result but i don’t understand why is that happening?

EDIT:
The question is more “Why does it happens this way”
I did some testing in ipython and that’s what i’ve got:

In [1]: d = {'a':'','b':'','c':''}

In [2]: d
Out[2]: {'a': '', 'b': '', 'c': ''}

In [3]: d['a'] = 'value'

In [4]: d['b'] = 'other_value'

In [5]: d['c'] = 'third_value'

In [6]: d
Out[6]: {'a': 'value', 'b': 'other_value', 'c': 'third_value'}

In [7]: d['a'] = 'inserting new value'

In [8]: d
Out[8]: {'a': 'inserting new value', 'b': 'other_value', 'c': 'third_value'}

So the value of the key could be updated and it changes, why doesn’t it happen in FOR loop?

  • 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-12T13:11:57+00:00Added an answer on June 12, 2026 at 1:11 pm

    Because your dic is created outside the loop, you only create one dict. If you want three different dicts, you need to create three different dicts, so move the initial creation of dic inside the loop.

    To answer your updated question, the issue is that although you think you are appending a new dict with each parsed.append(dic), you are just appending the same dict three times. Append doesn’t copy the dict. So whenever you modify that dict, all the dicts in parse show the change, since they are all the same dict. This version of your second code example may be more illustrative:

    >>> d = {'a': '', 'b': '', 'c': ''}
    >>> stuff = []
    >>> stuff.append(d)
    >>> print stuff
    [{'a': '', 'c': '', 'b': ''}]
    >>> d['a'] = 'other'
    >>> print stuff
    [{'a': 'other', 'c': '', 'b': ''}]
    >>> stuff.append(d)
    >>> print stuff
    [{'a': 'other', 'c': '', 'b': ''}, {'a': 'other', 'c': '', 'b': ''}]
    >>> d['a'] = 'yet another'
    >>> print stuff
    [{'a': 'yet another', 'c': '', 'b': ''}, {'a': 'yet another', 'c': '', 'b': ''}]
    

    Notice that changing the dict “works” in that it indeed changes the value, but regardless of that, the list still contains the same dict multiple times, so any changes you make overwrite whatever changes you made earlier. In the end, your list only contains the last version of the dict, because all earlier changes were overwritten in all dicts in the list.

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

Sidebar

Related Questions

My index.html page for my project needs some Ruby code so I need to
I am working on some project that needs that when ever a friend of
I'm working on a statistical project that involves iterating over every possible way to
I am just starting up a new project that needs some cross-platform GUI, and
My database project needs to include a SQL file with a command that will
In VS, it's simple. Everything the project needs is stored in the project folder
I'm working on a project which needs to use youtube api. The html and
I have a Java project that needs a addon interface. I was thinking about
I'm working on a project that needs to have a list of weekdays. I
I'm building a pet project which needs to convert pdf and ppt files to

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.