Python Forum
Theory behind referencing a dictionary rather than copying it to a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Theory behind referencing a dictionary rather than copying it to a list
#1
Hi there, I ran into a problem working with appending a dictionary to a list today. I eventually managed to figure out that appending a dictionary to a list without copying it is just appending a reference to that dictionary. Anyway I fixed it but I don't understand why:

In this code (the broken code), when I eventually print out my list I just get the same dictionary rather than different ones, which I understand happens because I referenced the dictionary rather than appending a copy to it each time.

labUpgradeDict = {}
for row in labTableRows:
		cells = row.find_all("td")
		for cell in cells:
			labUpgradeDict[labTableHeaderSimplified[cells.index(cell)]] = cell.getText().replace('\n', '').strip()
			labUpgradeList.append(labUpgradeDict)
Anyway I eventually changed my code to this, (changed the indent of the append for different reasons) and set the dictionary to being empty in the for loop.

for row in labTableRows:
		labUpgradeDict = {}
		cells = row.find_all("td")
		for cell in cells:
			labUpgradeDict[labTableHeaderSimplified[cells.index(cell)]] = cell.getText().replace('\n', '').strip()
		labUpgradeList.append(labUpgradeDict)
Now this worked but I don't understand why because I'm still referencing the dictionary rather than appending a copy. Each entry in my final list had a different dictionary yet I'm just appending a reference to it so I thought that the final list would just contain a recurring dictionary. Yet it doesn't

This code over here is what I would think is correct:

for row in labTableRows:
		labUpgradeDict = {}
		cells = row.find_all("td")
		for cell in cells:
			labUpgradeDict[labTableHeaderSimplified[cells.index(cell)]] = cell.getText().replace('\n', '').strip()
		labUpgradeList.append(labUpgradeDict.copy())
And it works in the same way as the code without the .copy().

Basically what I'm asking is in the first code. The whole idea of referencing breaks it BUT in the second code it works where I changed nothing but indentation and moving where I reset the dictionary, something which I'd think have no effect on referencing the dictionary, yet all of a sudden I get different copies of the same referenced dictionary unlike before where I just got a recurring copy.
Reply
#2
Moving the dict instantiation into the loop fixed it. Previously, you were referencing the same dict multiple times in a list. That means any update to the dict will update every reference in the list because they all get their information from the same memory location.

Inside the loop, the dict instantiation creates a new dict stored at a different memory location each time. Even though you're appending the same variable to the list, that variable has a different reference every time the loop runs.

out= []
for x in range(10):
    example = {}
    out.append(example)
    print(id(example))
Output:
52431536 55345824 55345728 55345680 55346208 55347456 55346256 55346304 55346352 55346400
Compared to the original:

out = []
example= {}
for x in range(10):
    out.append(example)
    print(id(example))
Output:
55345968 55345968 55345968 55345968 55345968 55345968 55345968 55345968 55345968 55345968
Reply
#3
Thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary in a list bashage 2 493 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 597 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Copying the order of another list with identical values gohanhango 7 1,060 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  Copy List Not Copying BAbdulBaki 3 574 Aug-19-2023, 02:03 AM
Last Post: perfringo
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  Queuing Theory in Python NOLA_Saints 0 749 Feb-22-2023, 11:42 PM
Last Post: NOLA_Saints
  check if element is in a list in a dictionary value ambrozote 4 1,879 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,896 Apr-28-2022, 06:59 AM
Last Post: buran
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,469 Apr-21-2022, 12:33 PM
Last Post: KatManDEW

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020