creating lists based on parsed items

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Wed Jun 6 20:34:55 EDT 2007


On Wed, 06 Jun 2007 22:49:27 +0000, Basilisk96 wrote:

>> If you are using Python 2.5, use a defaultdict instead, the very first
>> example looks like what you want.
>> <http://docs.python.org/lib/defaultdict-objects.html>
>>
>> --
>> Gabriel Genellina
> 
> Uh-oh..
> I am using Python 2.5 on WinXP, but when I tried the examples in the
> manual, I got a "NameError: name 'defaultdict' is not defined".  What
> am I missing in my Python installation?


This would be a RTFM moment :) defaultdict is not a built-in, it is part
of the collections module.

You have to import collections first.

>>> defaultdict
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'defaultdict' is not defined
>>> import collections
>>> collections.defaultdict
<type 'collections.defaultdict'>



-- 
Steven.




More information about the Python-list mailing list