0

I've declared some dicts that contain keys for each NFL team and an object related to them.

Now I'm trying to modify those dicts in order to have a key called 'teams' where I would put the previous values of the dict, and 5 new keys named 2018 to 2022

tmp = ['Packers', 'Vikings', 'Lions', 'Bears']
NFC_North = {name: team(name=name) for name in tmp}

tmp = ['Buccaneers', 'Saints', 'Panthers', 'Falcons']
NFC_South = {name: team(name=name) for name in tmp}

tmp = ['Cowboys', 'Giants', 'Eagles', 'Commanders']
NFC_East = {name: team(name=name) for name in tmp}

tmp = ['Rams', 'Cardinals', 'Seahawks', '49ers']
NFC_West = {name: team(name=name) for name in tmp}

NFC = [NFC_North, NFC_South, NFC_East, NFC_West]

I've tried implementing a for loop that iterates over every team and does what I described before. But when I check if the dict was modified, it reimained the same

print(NFC[0])

for i in NFC:
    i = {
        'teams': i,
        '2022': 0,
        '2021': 0,
        '2020': 0,
        '2019': 0,
        '2018': 0
    }
    print(NFC_North, i)

I checked the id's and I realized that items[i](suppose i = 0) is not the same object as NFC_North. Even though NFC_North is actually the first element in the NFC list

My code works as expected inside the loop, but I can't modify the actual element I want to modify and I don't know why it doesen't get modified and how can I do to modify it

I'm new to programming and this is my first time posting here, hope I wrote it in the right way. Thanks to you all in advance

Edit: Many of you are recommending me to do the following:

for i in range(len(NFC)):
    NFC[i] = {
        'teams': NFC[i],
        '2022': 0,
        '2021': 0,
        '2020': 0,
        '2019': 0,
        '2018': 0
    }

But the output remains the same. Correct me if I'm doing something wrong but I think this is the solution everybody is redirecting me towards, it's behaving just the same

mactatum
  • 1
  • 2
  • 1
    You're looping over the values, which don't modify the original container. Use `for i in range(len(NFC)): NFC[i] = ...`. – B Remmelzwaal Mar 01 '23 at 16:20
  • 1
    Related/possible duplicate: [Are Python variables pointers? Or else, what are they?](https://stackoverflow.com/q/13530998/11082165) – Brian61354270 Mar 01 '23 at 16:20
  • @BRemmelzwaal please take I look to what I just edited. I did what you suggested but my output remains the same – mactatum Mar 01 '23 at 16:59
  • Works perfectly when I apply the fix, adding the dummy function `def team(name): return name`. What output are you getting? – B Remmelzwaal Mar 01 '23 at 17:02
  • @Brian not really. I tried looping through the indexes as one answer on that post suggest and my output remains the same. I've just edited it on the post, maybe I'm writting something wrong – mactatum Mar 01 '23 at 17:04
  • @BRemmelzwaal my output is: {'Packers': <__main__.team object at 0x00000268CAEA06D0>, 'Vikings': <__main__.team object at 0x00000268CAEA0130>, 'Lions': <__main__.team object at 0x00000268CAEA0370>, 'Bears': <__main__.team object at 0x00000268CAEA0040>} When I expected: {'teams': {'Packers': <__main__.team object at 0x00000268CB819970>, 'Vikings': <__main__.team object at 0x00000268CB819BE0>, 'Lions': <__main__.team object at 0x00000268CB819B20>, 'Bears': <__main__.team object at 0x00000268CB819340>}, '2022': 0, '2021': 0, '2020': 0, '2019': 0, '2018': 0} – mactatum Mar 01 '23 at 17:06
  • Is there any other code which you haven't shown which would cause this discrepency? What is the `team()` function? – B Remmelzwaal Mar 01 '23 at 17:10
  • @BRemmelzwaal here it is class team: def __init__ (self, name): self.name = name self.AP = { 2018: 0, 2019: 0, 2020: 0, 2021: 0, 2022: 0 } I've written other lines but it is just defining the divisions and assigning values to teams.AP – mactatum Mar 01 '23 at 17:14
  • @BRemmelzwaal i revised it again and i've tried by printing NFC[0] and it works well. the issue was when i printed NFC_North(which is the variable listed as index 0 in NFC). I defined NFC as = [NFC_North, NFC_East, NFC_West, NFC_South]. So i'm supposing that creates a copy of the elements, not their pointer. I'm still having issue because i want to call NFC_North by its name, not by iterating over NFC. Is there any solution? This would have been much easier in C lmao – mactatum Mar 01 '23 at 17:31
  • That's actually not what happens: the initial list declaration _does_ have references to those dictionaries, but in the loop you are reassigning the list indices to be new values. Indeed, this would be much easier in C, because I still find references and copies in Python to be quite arbitrary. However, it _would_ work with `NFC[i].update()`, but you would need to change `'teams': NFC[i]` as that is a circular reference. – B Remmelzwaal Mar 01 '23 at 17:39

0 Answers0