0

I was attempting to make an empty dictionary and use set default to allow duplicates. For some reason, the only variable name that would work is “d1”. Is there a reason why? I had another friend with way more experience in Python programming tell me to change it to d1, but I don’t know why.

I tried newdict and even d2 as a placeholder name. Neither would show as variables even after being assigned to an empty dictionary.

  • 4
    You can use any name you want for your variables. Please show your code that's not working, along with the full traceback of the error. – Barmar Feb 10 '23 at 18:09
  • 3
    Can you add your code where you are initializing the dictionaries? – Orion447 Feb 10 '23 at 18:10
  • 3
    Have you tried asking your friend? – Matiiss Feb 10 '23 at 18:13
  • I did ask my friend, but he didn’t really offer an explanation! I’m new to coding so maybe it was common sense for him? – azhauri Feb 10 '23 at 18:28
  • I don’t have current access to my code, but I was combining a list of keys and values (some keys assigned to multiple values) using set default code. I kept an empty list for my new dictionary d1 = {} and later did d1.setdefault – azhauri Feb 10 '23 at 18:30
  • I would share my code but I’m not sure how academic integrity stuff works for coding :( – azhauri Feb 10 '23 at 18:31
  • @azhauri I don't think we can reproduce the issue without *some* of your code, and if we can't reproduce it, it's that much harder to help...can you share enough for a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) at least? – Driftr95 Feb 10 '23 at 19:47
  • d1 = {} if len(keys) == len(vals): for vals, keys in zip(range(len(keys)), keys): d1.setdefault(keys, []).append(vals) print(d1) – azhauri Feb 10 '23 at 22:47
  • That’s the code^^. I couldn’t change the name d1 at all and have it be recognized! – azhauri Feb 10 '23 at 22:48

0 Answers0