Weird errors when trying to access a dictionary key

Ben Finney bignose+hates-spam at
Sat Jul 21 01:18:54 CEST 2007

robinsiebler at writes:

> I have a data structure that looks like this:
> [...]
> rpt_file.writelines('\t' + [song].keys() + '\t' + [song].values() + '\n')

Forms the list [song] twice, attempting to call the 'keys()' method
and the 'values()' method of that list in turn...

> I get the following error:
> Traceback (most recent call last):
> [...]
> AttributeError: 'list' object has no attribute 'keys'

... which doesn't exist.

> Here is where it gets weird:
> song
> {"I Don't Wanna Stop": 1}
> song.keys()
> ["I Don't Wanna Stop"]

Yes. A list doesn't have a 'keys' or 'values' method, a dict
does. 'song' is a dict. So why are you not calling the 'keys' method
of the 'song' object?

 \           "If [a technology company] has confidence in their future |
  `\      ability to innovate, the importance they place on protecting |
_o__)  their past innovations really should decline."  -- Gary Barnett |
Ben Finney

More information about the Python-list mailing list