Lists: Examples


#Examples: Lists: methods and operations
#Remember lists start with item #0 - not item #1
print("You will need to look at the code along side the print out")
#CREATE A LIST
A_str = "Apple"
A_int = 1
A_dec = 1.4142136
print("List can include any type of literal or variable value mixed and matched.")
print("But when we create the list it records the value of a variable not the variable.")
print("Our list currently includes both of several types, one is variable A_str which = 'Apple':")
A_list = ["Orange", 2, 3.14159, A_str, A_int, A_dec]
print(A_list,'\n')

#USING VARIABLE CAUSES A DEEP COPY - NOT A SHALLOW COPY
print("We will change 'Apple' to 'Pear' in the A_str variable and we get...")
A_str = "Pear"
print("A_str is: ", A_str)
print("but our list is still ")
print(A_list)
print("This demos that making a list with other variables causes a 'deep' copy.")
print("That is geek speak for the value is copied, not just a pointer to the variable. \n")

#REMOVE AND INSERT ITEMS
print("We can remove an item by value, then replace an item by location")
print("Redefining A_list so the example is easier to see, A_list is now:")
A_list = ["Apple","Bannana", "Cherry", "Date","Elderberry","Fig"]
print(A_list)
print("Remove apple then insert pear as item 3 (0,1,2,3) and we get")
A_list.remove("Apple")
A_list.insert(3, "Pear")
print(A_list)
print("or we can use a slice insert of Grape at [1] and get:")
A_list[1]= "Grape"
print (A_list)
print()

#SLICE-REPLACE
print("Using a slice-replace like A_list[i:j] is a little tricky")
print("because your replacement has to be an 'iterable', like another list")
B_list =["Blackberry","Coconut","Tomato","Peach"]
print("We have a new list called B_list: ")
print (B_list)
print("A_list Before")
print(A_list)
A_list_copy = [] #ignore this - just making a deep copy for a moment
A_list_copy = A_list[:]
print("Replace 3 items of A_list with B_list")
A_list[2:5] = B_list
print("After Replacement A_list is: ")
print(A_list)
print("so you took out the three you asked to replace but put in the whole of B_list")
print("you have to slice your replacement list to define what you want")
#restore A_list
A_list = A_list_copy[:] #get our old A_list back
print("restored A_list: ", A_list)
A_list[2:6] = B_list[1:4] # when slicing, start is the item you want, end is the number after the last item you want#
#removing 4 items 2:6 (actually taking out 2,3,4,and 5); adding the last 3 items in B 1:4
#remember 0 is the first item
print ("newly sliced replacing [2:6] with [1:4]")
print (A_list, '\n')

#ITERATING LETTERS TO A LIST, DEL, REVERSE,
print("If you slice-replace a word, you get its letters as unique items; our testword is 'doorstop'")
print("See the code to understand how we built the letter list")
Testword = "doorstop"
Letter_list = [] #have to define the list before you use it
Letter_list[0:]= Testword
print("Here is our new letter list: ")
print(Letter_list)
print("While we are at it we will make two copies, one 'deep' and one 'shallow'")
LL_deep=[]
LL_deep = Letter_list[:] #some older heads frown on this but I think it is easier for beginners
LL_shallow=Letter_list # many programmmers perfer to import the copy module
print ("Slice delete the first 4 letters in the list")
del Letter_list[0:4]
print(Letter_list)
print("Reverse the last four letters")
Letter_list.reverse()
print(Letter_list)
print("Append adds one item at a time to the end of your list - add 'h' for example")
Letter_list.append("h")
print(Letter_list)
print("But it is not that hard to add groups of letters, lets add 'o' and 't'")
Letter_list[len(Letter_list):len(Letter_list)+2]=["o","t"]
print (Letter_list)
print("or we can use the extend command: ")
Letter_list.extend(["t","e","r"])
print(Letter_list)
print("You can also sort the list alphabetically")
Letter_list.sort()
print(Letter_list)
print("and here are our deep and shallow copies: ")
print("deep: ",LL_deep, " unchanged from the original because it copied values")
print("shallow: ", LL_shallow," points to same object so reflects all changes \n" )

#USING A LIST AS A STACK WITH POP
print("The 'pop([i])' command returns item i and removes it from the list.")
print("Without a value in the [] barckets it takes the last item in the list.")
print("We still have these fruits in A_list.", A_list)
print ("Lets pop a few: ")
for i in range(1,4):
    print("pop!")
    print(A_list.pop())
    print(A_list)