How to iterate throuhg dictionary
Steve Holden
sholden at holdenweb.com
Wed Aug 8 11:25:39 EDT 2001
"Mark Robinson" <m.1.robinson at herts.ac.uk> wrote in message
news:mailman.997279753.25222.python-list at python.org...
> I just had a quick play with that, and I can't see any difference
> between your example verbatum and simply print d. How does pprint work
> and (I assume it stands for pretty print or something) how should it look?
>
As always, the fine manual is an excellent guide. Since you don't appear to
have one handy, here are a few examples to whet your appetite.
>>> import pprint
>>> d = {1: ['a', 'b', (1,2,3,4), ['e','f', 'g']],
... 2: "J Random User",
... 3: ('tuple', ('with', ('lots', ('of', 'nesting'))))
... }
>>> pprint.pprint(d)
{1: ['a', 'b', (1, 2, 3, 4), ['e', 'f', 'g']],
2: 'J Random User',
3: ('tuple', ('with', ('lots', ('of', 'nesting'))))}
>>> pp = pprint.PrettyPrinter(indent=5, width=60)
>>> pp.pprint(d)
{ 1: ['a', 'b', (1, 2, 3, 4), ['e', 'f', 'g']],
2: 'J Random User',
3: ('tuple', ('with', ('lots', ('of', 'nesting'))))}
>>> for k in d.keys():
... pp.pprint(d[k])
...
('tuple', ('with', ('lots', ('of', 'nesting'))))
'J Random User'
['a', 'b', (1, 2, 3, 4), ['e', 'f', 'g']]
>>> pp = pprint.PrettyPrinter(indent=5, width=20)
>>> pp.pprint(d)
{ 1: [ 'a',
'b',
( 1,
2,
3,
4),
[ 'e',
'f',
'g']],
2: 'J Random User',
3: ( 'tuple',
( 'with',
( 'lots',
( 'of',
'nesting'))))}
>>> for k in d.keys():
... pp.pprint(d[k])
...
( 'tuple',
( 'with',
( 'lots',
( 'of',
'nesting'))))
'J Random User'
[ 'a',
'b',
(1, 2, 3, 4),
[ 'e',
'f',
'g']]
>>>
regards
Steve
--
http://www.holdenweb.com/
More information about the Python-list
mailing list