#!/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('<pre>')                          #to produce better display on the web 

list1 = [0,1,2,3,4,5,6,7,8,9]                                   #a single numeric list

list2 = ['dog', 'cat' ,'pig', 'cow', 'duck', 'bird' ]           #a single string list       

list3 = [[1,'Barbara','Burns  ',  '000-01-0001','F'],           #a 2 dimensional list 
         [2,'Vincent','Cambria',  '000-01-0002','M'], 
         [3,'Duncan ', 'Davidson','000-01-0003','M']]

print('The entire list           \t\t', list1)
print('The length of the list    \t\t', len(list1))
print('Element 0 and element 1   \t\t', list1[0], 'and', list1[1])
print('Elements 1 through 5 only \t\t', list1[1:6])
list1[0] = 'zero'
list1.append(10)                                                #append to end of list
list1.insert(4, 3.1)                                            #insert an element at position 4
print('list1 after change, insert & append \t', list1)
del list1[4]                                                    #delete element at position 4                                           
print('list1 after delete 4th element      \t', list1)
list1.remove(10)                                                #remove element with value 10
print('list1 after removing element 10     \t', list1)

print("looping through list1 using 'for in'     ", end='')
for element in list1:
    print(element, '\t', end='')
print()

print("looping through list1 using 'for range'  ", end='')
for idx in range(len(list1)):
    print(list1[idx], '\t', end='')
print()


print()
print('The entire list \t\t\t', list2 )
found = 'cat' in list2                                          #determine T/F if 'cat' is found in the list
pos   = list2.index('cat')                                      #determine what index 'cat' is in the list

print('Does element value="cat" exist?        \t', found, ', in position:', pos)

string = '_'.join(list2)                                        #join all the elements using '_' and return a string
print('Converting the list to a string        \t', string)
list2a = string.split('_')                                      #split a string using '_' and return a list
print('Converting the string back to a list   \t', list2a)
list2.sort()                                                    #sort a list ascending
print('Sorting list2 list in ascending order  \t', list2)
list2.sort(reverse=True)                                        #sort a list descending
print('Sorting list2 list in descending order \t', list2)


print()
newlist = list1 + list2                                         #concatenate 2 lists
print('Concatenating list1 and list2    \t', newlist)
newlist += ['another_element']                           		#concatenate list with another element (must be a list)
print('Concatenating list1 and list2    \t', newlist)


print()
print('The entire 2 dimensional list3   \t', list3 )
print('Row 1 only                       \t', list3[0] )
print('Row 2, column 5                  \t', list3[1][4] )
print('The length of entire list3       \t', len(list3) )
print('The length of list3 row 1 only   \t', len(list3[0]) )

list3a = sorted(list3)                                          #sort 2 dimensional list ascending on element 0 
print('Sorting list3 on column 0 ascending    \t', list3a) 
list3a = sorted(list3, reverse=True)                            #sort 2 dimensional list descending on element 0
print('Sorting list3 on column 0 descending   \t', list3a)

print()
print('Looping thru 2 dimensional list3...')
for row in list3:   
    for col in row:
        print(col, '\t', end='')
    print()
print()

print('Looping thru 2 dimensional list3 another way...')
for idxR in range(len(list3)):   
    for idxC in range(len(list3[idxR])):
        print(list3[idxR][idxC], '\t', end='')
    print()
print()