Page 1 of 1

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

Posted: Sun Feb 13, 2011 8:28 am
by Isaac

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!

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

Posted: Sun Feb 13, 2011 5:25 pm
by Jeff250
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', [])

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

Posted: Sun Feb 13, 2011 6:41 pm
by Isaac
edit:
will get back to you on this...
Thanks for showing me get()!

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

Posted: Fri Feb 18, 2011 10:47 pm
by Isaac

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!

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

Posted: Sat Feb 19, 2011 1:44 am
by Jeff250
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]