Python: Building a list within the value of a dictionary.
Posted: 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 think this solution is really cool!