#!/usr/bin/env python3
#=====================================================================================
# Python dictionaries and nested dictionaries
#=====================================================================================
print("Content-Type: text/html \n")     #http header with newline char (required for the web)
print('<pre>')                          #to produce better display on the web 


stuff1 = {'first':'Sam', 'last':'Sultan', 'gender':'M'}

stuff2 = {1:'one', 2:'two', 3:'three', 4:'four', 5:'five', 6:'six'}

stuff3 = [{'id':1, 'fname':'Barbara', 'lname':'Burns  ',  'ssn':'000-01-0001', 'sex':'F'}, 
          {'id':2, 'fname':'Vincent', 'lname':'Cambria',  'ssn':'000-01-0002', 'sex':'M'}, 
          {'id':3, 'fname':'Duncan ',  'lname':'Davidson','ssn':'000-01-0003', 'sex':'M'}]

print()
print('The entire dictionary as coded            ', "{'first':'Sam', 'last':'Sultan', 'gender':'M'}")
print('The entire dictionary as per Python print ', stuff1, ' <-- Order is not guaranteed')
print('The number of elements in dictionary      ', len(stuff1))
print("The content of element 'gender'           ", stuff1['gender'])
stuff1['first']  = 'Samuel'
stuff1['id']     = '12345678'
stuff1['role']   = 'instructor'
print('stuff1 after change and insert            ', stuff1)
del stuff1['role']
print("stuff1 after delete element 'role'        ", stuff1)

print("looping through stuff1 using 'for in'      ", end='')
for key in stuff1:
    print(key, '->', stuff1[key], '\t', end='')
print()


print()
print('The entire stuff2 dictionary       \t', stuff2, ' <-- Order is not guaranteed')
stuff2_keys = stuff2.keys()
print('The keys only                      \t', stuff2_keys)
stuff2_vals = stuff2.values()
print('The values only                    \t', stuff2_vals)
stuff2_key_asc  = sorted(stuff2.keys())
print('Sorting stuff2 by keys ascending   \t', stuff2_key_asc)
stuff2_key_asc  = sorted(stuff2.keys(),reverse=True)
print('Sorting stuff2 by keys descending  \t', stuff2_key_asc)
stuff2_val_asc  = sorted(stuff2.values())
print('Sorting stuff2 by values ascending  \t', stuff2_val_asc)
stuff2_val_desc = sorted(stuff2.values(),reverse=True)
print('Sorting stuff2 by values descending \t', stuff2_val_desc)


print()
print('A list containg a dictionay   \t', stuff3)
print('Row 1 only                    \t', stuff3[0] )
print('Row 2, last name              \t', stuff3[1]['lname'] )
print('The length of stuff3          \t', len(stuff3) )
print('The length of row 1           \t', len(stuff3[0]) )


print()
print('Looping thru stuff3...')
for row in stuff3:
    for key in row:
        print(row[key], '\t', end='')
    print()
print()