Posts

Rewrite insert() function - list index out of range

Image
1 I'm essentially trying to re-create a version of the insert() function for a homework assignment. I've written the following function: def insert_value(my_list, value, insert_position): list_copy = if insert_position < 0: insert_position = 0 if insert_position >= len(my_list): insert_position = len(my_list) - 1 i = 0 while i < len(my_list) + 1: while i < insert_position: list_copy.append(my_list[i]) i += 1 while i == insert_position: list_copy.append(value) i += 1 while i > insert_position: list_copy.append(my_list[i-1]) i += 1 return list_copy I get the error list index out of range for the final while sub-loop, and I can...