def myCount(myList,item): count = 0 for thing in myList: if thing == item: count += 1 #All done with the loop return count def myIndex(myList,item): index = 0 for thing in myList: if thing == item: return index index += 1 return -1 ### Alternate way ## for index in range(len(myList)): ## if myList[index]==item: ## return index ## ## return -1 def contains(myList,item): for thing in myList: if thing == item: return True #Didn't return, so didn't find item return False def myInsert(myList,index,item): #Check bad case if index < 0: return myList # Use slicing return myList[:index] + [item] + myList[index:] ### Alternate way ## outList = [] ## ## #if index is beyond end of myList, copy myList into ## #outList and append item to the end of outList ## if index >= len(myList): ## outList = myList[:] #full slice ## outList.append(item) ## return outList ## ## #otherwise, loop ## for position in range(len(myList)): ## if position == index: ## outList.append(item) ## ## #copy element from myList into outList ## outList.append(myList[position]) ## ## return outList ## def myReverse(myList): outList = [] for thing in myList: outList = [thing] + outList return outList ### Alternate way ## for index in range(len(myList)-1),-1,-1): ## outList.append(myList[index]) ## ## return outList