#!/usr/bin/env python3
#=====================================================================================
# Python lists and nested lists (A list is an array in most languages)
#=====================================================================================
print("Content-Type: text/html \n")     #http header with newline char (required for the web)

print('''
    <html>
    <body>
    <h1>Python List Functions</h1>
''')

list1 = [0,1,2,3,4,5,6,7,8,9]

list2 = ['dog', 'cat' ,'pig', 'cow', 'duck', 'bird' ]

list3 = [[1,'Barbara','Burns',   '000-01-0001','F'],            #2-dimensional list 
         [2,'Vincent','Cambria', '000-01-0002','M'], 
         [3,'Duncan', 'Davidson','000-01-0003','M']]
         
list4 = [[ 'x' for col in range(5)] for row in range(5)]        #load all elements with 'x'

list5 = [[ col for col in range(5)] for row in range(5)]        #load all elements with col values


### Manipulate list 1 ################################################
print('<table border=1>')
print('<tr><th colspan=2 bgcolor=tan>list1')
print('<tr><td>The entire list           <th>', list1)
print('<tr><td>The length of the list    <td>', len(list1) )
print('<tr><td>Element 0 and element 1   <td>', list1[0], 'and', list1[1] )
print('<tr><td>Elements 1 through 5 only <td>', list1[1:6] )
list1[0] = 'zero'
list1.insert(4, 3.01)
list1.append(10)
print('<tr><td>list1 after change, insert & append  <td>', list1)
del list1[4]
print('<tr><td>list1 after delete 4th element <td>', list1)
list1.remove(10)
print('<tr><td>list1 after delete element 10  <td>', list1)
print('</table>')
print('<br>')


### Manipulate list 2 ################################################
print('<table border=1>')
print('<tr><th colspan=2 bgcolor=tan> list2')
print('<tr><td>The entire list <th>', list2 )
found = 'cat' in list2
pos   = list2.index('cat')
print("<tr><td>Does element value='cat' exist?      <td>", found, ", in position:", pos)
string = '_'.join(list2)
print('<tr><td>Converting the list to a string      <td>', string)
list2a = string.split('_')
print('<tr><td>Converting the string back to a list <td>', list2a)
list2.sort()
print('<tr><td>Sorting list2 list ascending         <td>', list2)
list2.sort(reverse=True)
print('<tr><td>Sorting list2 list descending        <td>', list2)
print('</table>')
print('<br>')


### Manipulate 2-dimensional list3 ################################################
print('<table border=1>')
print('<tr><th colspan=2 bgcolor=tan>list3 (2 Dimensional)')
print('<tr><td>The entire 2 dimensional list <th>', list3 )
print('<tr><td>Row 1 only                    <td>', list3[0] )
print('<tr><td>Row 2, column 5               <td>', list3[1][4] )
print('<tr><td>The length of list3 list      <td>', len(list3) )
print('<tr><td>The length of list3 row 1     <td>', len(list3[0]) )

new_list = [4,'Sam', 'Sultan','000-01-0004','M']
list3.append(new_list)                                                  #add entire row to list3

print('<tr><td>Adding a new inner list to list3<td>', new_list) 

list3a = sorted(list3, key=lambda list3: list3[1]) 
print('<tr><td>Sorting list3 on column 2 ascending <td>', list3a) 

list3a = sorted(list3, key=lambda list3: list3[1], reverse=True)
print('<tr><td>Sorting list3 on column 2 descending <td>', list3a)
print('</table>')
print('<br>')

print('<table border=1>')
print('<tr><th colspan=5 bgcolor=tan>Looping thru 2dim list</th></tr>')     #loop thru 2-dim list
for row in list3:
    print('<tr>', end='')
    for col in row:
        print('<td>', col, end='')
    print('</tr>')
print('</table>')
print('<br>')

print('<table border=1>')
print('<tr><th colspan=5 bgcolor=tan>Looping thru list4 (pre-loaded 2dim list</th></tr>')
for row in list4:
    print('<tr>', end='')
    for col in row:
        print('<td>', col, end='')
    print('</tr>')
print('</table>')
print('<br>')

print('<table border=1>')
print('<tr><th colspan=5 bgcolor=tan>Looping thru list5 (pre-loaded 2dim list</th></tr>')
for row in list5:
    print('<tr>', end='')
    for col in row:
        print('<td>', col, end='')
    print('</tr>')
print('</table>')
print('<br>')

print(''' 
    </body> 
    </html> 
''')
      


#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================