Python lists, increment & return modified list -
this question has answer here: python assign values list elements in loop 4 answers the below code doesn't work intended: l = [1,1] def modifylist(list): element in list: element += 1 return list the list expect when run: modifylist(l) is [2,2] instead of [1,1] can explain why python acts way? you need access list element using index , increment: def modifylist(lst): ind, _ in enumerate(lst): lst[ind] += 1 # access list element index , increment return lst or use list comprehension , add 1 each element: def modifylist(lst): lst[:] = [ele +1 ele in lst] # lst[:] changes original list return lst if modifying original list in place, don't need return value, if want create new list return list comprehension: return [ele +1 ele in lst] def mod_in_place(lst): lst[:] = [ele +1 ele in lst] # lst[:] chan...