# examples for Lists # Tracing through code print("---- Tracing through code ----") num = 5 grades = [97, 86, 91, num, 88] num = 33 big = [23, grades, num, 7] grades[1] = 87 grades.append(6) print("grades =", grades) big[2] = grades print("big =", big) print() # print an empty line to separate sets of examples # Creating Lists print("---- Creating Lists ----") animals = ['cow', 'dog', 'horse'] # create a new list print("animals =", animals) animals1 = [] # create an empty list print("animals1 =", animals1) animals1 = ['cow', 'horse'] animals2 = ['dog'] animals3 = animals1 + animals2 # concatenate lists print("animals3 =", animals3) # Accessing Items in Lists print("---- Accessing Items in Lists ----") animals = ['cow', 'dog', 'horse'] # create a new list print("There are", len(animals), "items in the list") print("animals[2] =", animals[2]) # access a particular item animals[2] = 'duck' # update a particular item print("updated animals[2] =", animals[2]) print("animals[0] =", animals[0]) # indices start from zero print("animals[-1] =", animals[-1]) # negative numbers start from the end of the list print('The ' + animals[0] + ' and the ' + animals[2] + ' sleep in the barn.') # Adding Items to Lists print("---- Adding Items to Lists ----") animals = ['cow', 'dog', 'horse'] # create a new list animals.append('deer') # add item to the end of the list print(animals[2]) # access a particular item animals[2] = 'duck' # update a particular item print(animals[0]) # indices start from zero print(animals[-1]) # negative numbers start from the end of the list print('The ' + animals[0] + ' and the ' + animals[2] + ' sleep in the barn.') animals.insert(2, 'pig') # insert element to a particular position of the list print(animals) # Removing Items from Lists print("---- Removing Items from Lists ----") animals = ['cow', 'dog', 'horse', 'sheep', 'pig'] print(animals) del animals[3] # delete an element, by index print(animals) animals.remove('horse') # remove a particular element, by value print(animals) # Length of Lists print("---- Length of Lists ----") animals = ['dog', 'cat', 'bird'] counter = 0 while counter < len(animals): print(animals[counter]) counter += 1 print(animals) print() print("---- index() ----") int_lst = [1, 2, 3] str_lst = ['1', '2', '3'] print(int_lst.index(2)) print(str_lst.index('2')) # print(str_lst.index(2)) # error, why? print() # in keyword print("---- in keyword ----") lst = [5, 7, 9, 11, 15] print(7 in lst) print(3 in lst) print(3 not in lst) print() # Slicing and returning part of a list with [ : ] print("---- slicing with [ : ] ----") lst = [5, 7, 9, 11, 15] print(lst) print(lst[1:4]) print(lst[1:]) print(lst[:4]) print(lst[:-1]) print(lst[1:-2]) print(lst[2:4]) print("type of sliced list =", type(lst[2:4])) print() # Sorting and Reversing print("---- Sorting and Reversing ----") animals = ['cow', 'dog', 'horse', 'sheep', 'pig'] animals.sort() print('sorted animals =', animals) print('sorted animals = ' + str(animals)) # another way to print (notice a space after "=") animals.reverse() print('reversed animals = ', animals) print() # Two Dimensional list (list of lists) print("---- Two Dimensional list (list of lists) ----") lst1 = [5, 7, 9, 11, 15] twoD_lst = [['cow', 'horse'], ['list'], [], [4, 5, 6], lst1] print(twoD_lst) print("the 0th item of twoD_lst =", twoD_lst[0]) # access a particular list print("the 1st item of the 0th item of twoD_lst =", twoD_lst[0][1]) # access a particular item print("len of twoD_lst =", len(twoD_lst)) print("len of 1st item of twoD_lst =", len(twoD_lst[1])) print() print('iterate 2D list with nested for loops') # iterate 2D list (there are many possible solutions) row = "" cnt = 0 # for each inner list for item in twoD_lst: # iterate an inner list for inner_item in item: # form string to print row += str(inner_item) + " " print(" twoD_lst[" + str(cnt) + "] =", row) row = "" # reset so there'd be no left over cnt += 1 # just to make the output easy to read print() # More examples print("---- More examples ----") letters = ['A', 'B', 'C'] numbers = [1, 2, 3] # concatenate lists of different element type both = letters + numbers print("concat " + str(letters) + " and " + str(numbers) + " = " + str(both)) # notice casting # letters, numbers, and both are lists. # here, we tried to mix strings and print lists, so casting is needed # if we simply print a list, for example print(both), casting is not needed print(letters * 3) # repeat lists del both[3] # delete a particular item (by index) both.remove('B') # remove by item print(both) print() ################################## print("---- create a copy of a list ----") # Create a copy of a list import copy animals = ['cow', 'horse', 'sheep', 'dog', 'chicken'] animals2 = animals animals.append('goat') animals3 = copy.copy(animals) animals3.append('farmer') print(animals3) print(animals) print() print("---- sort() and reverse() ----") animals.sort() print('sorted animals = ', animals) animals.reverse() print('reversed animals = ', animals) print() print("---- create a list from a user's inputs ----") # Create a list from a use's inputs names = [] names.append(input("Give me a name: ")) names.append(input("Give me another name: ")) print(names) print() print("---- max, min(), sum() of list of numbers ----") # max, min, sum list1 = [12, 14, 6.0, 7, 10.5] print("given list1 =", list1) print("max(list1)=", max(list1)) print("min(list1)=", min(list1)) print("sum(list1)=", sum(list1)) print() print("---- max(), min(), sum() of list of characters") list2 = ["x", "y", "a", "b"] print("max(list2)=", max(list2)) print("min(list2)=", min(list2)) # print("sum(list2)=", sum(list2)) # error, why? print() print("---- max(), min(), sum() of list of mixed types ----") list3 = ["x", 12, 6.0, "b"] # print("max(list3)=", max(list3)) # error, why? # print("min(list3)=", min(list3)) # error, why? # print("sum(list3)=", sum(list3)) # error, why?