Python: Building a list within the value of a dictionary.

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7128
Joined: Mon Aug 01, 2005 8:47 am
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝

Python: Building a list within the value of a dictionary.

Post by Isaac » Sun Feb 13, 2011 8:28 am

Code: Select all

>>> dic.setdefault("test",[]).append("new")
>>> dic
{'test': ['new']}
>>> dic.setdefault("test",[]).append("blue")
>>> dic
{'test': ['new', 'blue']}
>>> dic.setdefault("test",[]).append("blank")
>>> dic
{'test': ['new', 'blue', 'blank']}
>>> dic.setdefault("test2",[]).append("blank")
>>> dic
{'test': ['new', 'blue', 'blank'], 'test2': ['blank']}
>>> dic.setdefault("test2",[]).append("red")
>>> dic.setdefault("test2",[]).append("white")
>>> dic.setdefault("test3",[]).append("white")
>>> dic
{'test': ['new', 'blue', 'blank'], 'test3': ['white'], 'test2': ['blank', 'red', 'white']}
>>> 
I asked for help on the python forum. I had trouble getting the values to update with out replacing each other or not being placed correctly.

I think this solution is really cool!
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6387
Joined: Sun Sep 05, 1999 2:01 am
Location: ☃☃☃

Re: Python: Building a list within the value of a dictionary

Post by Jeff250 » Sun Feb 13, 2011 5:25 pm

It's funny you mention this. I had the same issue a month or so ago and stumbled upon this solution via Google on the python mailing lists. Like you said, you could implement this pattern without the built in setdefault() method, but the code gets unnecessarily nasty. This probably goes without saying, but then if you want to get with a default without necessarily setting the default, you can just say:

Code: Select all

dict.get('test', [])
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7128
Joined: Mon Aug 01, 2005 8:47 am
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝

Re: Python: Building a list within the value of a dictionary

Post by Isaac » Sun Feb 13, 2011 6:41 pm

edit:
will get back to you on this...
Thanks for showing me get()!
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7128
Joined: Mon Aug 01, 2005 8:47 am
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝

Re: Python: Building a list within the value of a dictionary

Post by Isaac » Fri Feb 18, 2011 10:47 pm

Code: Select all

>>> dic.get(str,[]).append("testa")
>>> dic
{}
>>> a=dic.get("as",[])
>>> a
[]
>>> a.append("test")
>>> a
['test']
>>> a=dic.get("as",[])
>>> a
[]
I'm very curious about this. However, I think I need to start my python training over from scratch. There are many fundamentals I think I've skipped over. A user on the python forum I go to offered this: http://openbookproject.net/thinkcs/python/english2e/

Thanks again!
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6387
Joined: Sun Sep 05, 1999 2:01 am
Location: ☃☃☃

Re: Python: Building a list within the value of a dictionary

Post by Jeff250 » Sat Feb 19, 2011 1:44 am

I doubt you have to relearn it from scratch.

dict.get(key, []) is to getting as dict.setdefault(key, []) is to setting. So get(key, default) just returns the default if nothing is in the dict, but doesn't put anything in it. But setdefault(key, default), before it returns the default, puts it in the dict for that key too. So although in this case they both return a [], only setdefault is returning a reference to a [] already inside the dict.

References are probably something to think more about. Since python has both functional and imperative idioms, it's not always clear which operations mutate lists or which create new ones. For example:

Code: Select all

>>> xs = [1,2,3]
>>> ys = xs
>>> xs.append(4)
>>> xs
[1, 2, 3, 4]
>>> ys
[1, 2, 3, 4]
But

Code: Select all

>>> xs = [1,2,3]
>>> ys = xs
>>> xs = xs + [4]
>>> xs
[1, 2, 3, 4]
>>> ys
[1, 2, 3]
And

Code: Select all

>>> xs
[1, 2, 3, 4]
>>> xs = [1,2,3]
>>> ys = xs[:]
>>> xs.append(4)
>>> xs
[1, 2, 3, 4]
>>> ys
[1, 2, 3]
Post Reply