# Example of building and returning a new list # # Returns a new list containing the negative numbers that appear in the # input list # def negativeListFrom(l): result = [] for number in l: if number < 0: #print("before", result) #result = result + [number] result.append(number) #print("after", result) return result # assume list1 and list2 are equal length lists of numbers # return a list where the ith item is the larger of the ith items # of list1 and list2 # def listOfBiggests(list1, list2): newList = [] index = 0 while index < len(list1): if list1[index] > list2[index]: newList = newList + [list1[index]] else: newList = newList + [list2[index]] index = index + 1 return newList def listOfBiggests2(list1, list2): newList = [] for index in range(len(list1)): item1 = list1[index] item2 = list2[index] if item1 > item2: newList.append(item1) else: newList.append(item2) return newList # listOfLists is a list containing 0 or more lists of numbers # Return a new list that is the same length as listOfLists but where # the ith element of the result is the average of all the numbers # in the ith element of listOfLists # # When does this work/not work? Is the specification clear enough to know? # def getAverages(listOfLists): averages = [] for sublist in listOfLists: # compute average for sublist sublistSum = 0 for item in sublist: sublistSum = sublistSum + item if len(sublist) == 0: averages.append(0) else: # append sublist average to result averages.append(sublistSum/len(sublist)) return averages def generatePairs(list1, list2): result = [] for item1 in list1: print("result so far:", result, "curr item1:", item1) for item2 in list2: print(item1, item2) #result = result + [ [item1, item2] ] result.append([item1,item2]) return result def sublistSums(listOfLists): result = [] for sublist in listOfLists: # add up numbers in sublist and append sum to result sum = 0 for number in sublist: sum = sum + number result = result + [ sum ] #result.append(sum) return result