A dictionary list?

Gary Herron gherron at islandtraining.com
Tue Dec 23 23:45:48 EST 2003


On Tuesday 23 December 2003 08:27 pm, Steve wrote:
> Hi,
>
> How can I create a list of a dictionary? (in other words, I need to
> create an array of a dictionary). Please help!
>
> Steve

That question is not very specific but perhaps dictionary methods
"key", "values", or "items" might be what you want:

>>> # Here is a sample dictionary
>>> d = {1:'one', 2:'two', 3:'three'}
>>>
>>> # Get a list of its keys
>>> d.keys()
[1, 2, 3]
>>>
>>> # Get a list of its values
>>> d.values()
['one', 'two', 'three']
>>>
>>> # Get a list of (key,value) tuples
>>> d.items()
[(1, 'one'), (2, 'two'), (3, 'three')]


Hope that helps,

Gary Herron







More information about the Python-list mailing list