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: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

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

Post 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!
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6511
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

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

Post 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', [])
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

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

Post by Isaac »

edit:
will get back to you on this...
Thanks for showing me get()!
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

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

Post 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!
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6511
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

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

Post 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]
Post Reply