Dict Help

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Tue Jul 17 11:57:29 CEST 2007

Morning Gabriel,

I'm looking for a little more advice on this dictionary/list to
defaultdict/set conversion that we were talking about, there were a few
things I was looking to clarify. Firstly, what is the difference between a
standard dict and a default dict? Is it purely a performance issue? 

This dict is likely to grow pretty large and is read/written on a very
regular basis so the better performing one is going to work best for me.
Also, am I still able to iterate over a set in the same way I can a list?

Here is an example of my add function at the moment, how can that be
converted to a defaultdict/set instead of the dict/list approach?

	self.History = {}

	def addHistory(self, address, media):
		if address not in self.History:
			self.History[address] = []


Thanks Gabriel,


-----Original Message-----
From: python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org
[mailto:python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org]
On Behalf Of Robert Rawlins - Think Blue
Sent: 15 July 2007 20:47
To: 'Gabriel Genellina'; python-list at python.org
Subject: RE: Dict Help

Thanks Gabriel,

That all works a charm, Iteration on this isn’t important for me so the SET
will work much better I think, I had read about them before but have never
used them, I'm glad to see it works so simply.



-----Original Message-----
From: python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org
[mailto:python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org]
On Behalf Of Gabriel Genellina
Sent: 15 July 2007 20:33
To: python-list at python.org
Subject: Re: Dict Help

En Sun, 15 Jul 2007 11:39:24 -0300, Robert Rawlins  
<robert_rawlins at hotmail.com> escribió:

> I'm looking for some help expanding on a dictionary I've got and storing  
> multiple values per key and the best way to do it. I'm guessing that I  
> need to store a list inside the value of the dictionary, but I'm not  
> quite sure how that can be achieved, and also how to check for values in  
> that list.
> Here is a brief example of what I want to be able to build:
> Key                                          Value
> 00:0F:DE:A8:AB:6F                      
> 00:17:B0:A0:E7:09                      
> 00:1B:98:16:21:E4                      
> 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg

There are a few ways to do it. Just translating your own words into  
Python, use a dictionary with a list of values.

> Now I really need to have two functions, one of which appends a value to  
> the list for a specified key, so I have the key and the new value as  
> strings, I then want to locate that key and append the list with the new  
> value.

The simplest way:

d = {}
# add a pair key, value
if key not in d:
   d[key] = [value]

> The second function I need, is to check for the existence of a value in  
> the list of a particular key, again I have the key and the value and I  
> want to do something like:
> if value in list of values for key:
>        do something here...

# check for a key, value
exists = key in d and value in d[key]

> The final function that would be helpful to me would to be able to  
> remove a value from the list for a specific key.

# remove value from key's values
if key in d and value in d[key]:

> I'm not sure if a list as the value in a dict is possible, or if its the  
> best way to achieve this, It just made logic sense to me so thought I'd  
> come and get your thoughts on this. If anyone has any better suggestions

It's perfectly possible as you can see. Now, depending on how many values  
you have, or how often do you test, you might replace the list with a set.  
Sets are unordered collections (you will loose the insertion order) but  
are better suited for a "contains" test (O(1) instead of O(n) for a list)
And you might replace the dictionary with a defaultdict. The insertion  
would become:

 from collections import defaultdict
d = defaultdict(set)
# add a pair key, value

The existence check and remove method do not change.

Dicts, lists and sets are documented here:  
<http://docs.python.org/lib/types.html> and defaultdict  

Gabriel Genellina



More information about the Python-list mailing list