>> How do i pretty print output of dictionary container? Sort of tabular
>> form or something, e.g.,
>>
>> 1. name1        email address1
>> 2. name2        email address2
>>
>> Just for my learning experience :-). Thanks!

[Liam Clarke]

highLength=0
for element in myDict.keys():
     if len(element) > highLength:
          highLength = len(element)

index = 0
minimumSpaces= 5
for (key, item) in myDict.items():
      index += 1
      spaceMult=(highLength+minimumSpaces)-len(key)
      outString=str(index)+". "+key+(spaceMult * " ") + item
      print outString

[/Liam Clarke]

This is what I had come up with. Where 'd' is a dictionary. This also assumes 
that when the display_name is input, that it is less than 25 characters in 
length.

####
def display_contacts(d):
    print '\nYou have %i contacts in your book.\n' % len(d)
    while 1:
        count = 0
        for item in d:
            display_name = item
            while len(display_name) < 25:
                display_name += '.'
            count += 1
            print count, display_name, d[item]

        raw_input('\nPress <Return> to continue.\n')
        break
####
>>> x = {'Justin Straube': '[EMAIL PROTECTED]',
     'Eri Mendz': '[EMAIL PROTECTED]',
     'Python-Tutor': '[EMAIL PROTECTED]',
     'Hello': 'World'}
>>> display_contacts(x)

You have 4 contacts in your book.

1 Justin Straube........... [EMAIL PROTECTED]
2 Hello.................... World
3 Eri Mendz................ [EMAIL PROTECTED]
4 Python-Tutor............. [EMAIL PROTECTED]

Press <Return> to continue.

>>>

regards,

Justin

---
Justin Straube
[EMAIL PROTECTED]
http://www.angelfire.com/wi3/phosphorescent/

Whatever you have thought about the world before,
forget it, now you are in this one

_______________________________________________
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to