I want to modify the list within a function, that will be later reflected onto the outer world so that it will change whenever we call a function. Yet, the list is unmodified.
def modify_in_place(a_list, start, end):
a_list= a_list[start:end:1]
list_A=[1,2,3,4]
modify_in_place(list_A,1,3)
print(list_A)
list_A should return [2,3] but it returns [1,2,3,4]
You can assign to the full slice of the list to modify it in-place.
def modify_in_place(a_list, start, end):
a_list[:] = a_list[start:end:1]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.