# File: ChapterLists/increment.py """Check if interger with isinstance(n,int) Increment each element in a list. Different methods """ def inclist(L): # increment all numbers in a list by index for i in range(len(L)): # be sure to convert list to an int list L[i] = L[i] + 1 return L def increment(L): # increment all numbers in a list r = [] for x in L: r.append(x + 1) return r def incMap(L): # apply the increment function to all return list(map(lambda x: (x + 1), L)) def onlyInts(L): # increment only numbers from a list res = [] for x in L: if isinstance(x,int): res.append(x + 1) return res def main(): # try all methods # reads a string then splits with blank and myL = input( "Input a list of integers( EX: 2 3 0[ENTER])?" ).split(" ") # makes ['2','3','0'] myL = [int(x) for x in myL ] # converts to a list of integers print(myL) print("increment list:", inclist(myL) ) #list has changed print("increment list:", increment(myL)) print("increment list:", incMap(myL)) # [6, 7] incremented list: [7, 8] print(onlyInts([1,2,'a','b',3])) #[2, 3, 4] main()