Your code example is wrong.
If you change then indentation to iterate over all elements, then you would get an
This modifies the list in-place and does return
- Indentation of return
- the use of range(len(element)), iterate directly
- alist is modified in-place and is also returned
- Iterating over a list and modifying the list in-place will throw an
IndexError
.
If you change then indentation to iterate over all elements, then you would get an
IndexError
.This modifies the list in-place and does return
None
:def remove_it(val, alist): remove = [] for index, element in enumerate(alist): if element == val: remove.append(index) for index in reversed(remove): alist.pop(index) my_list = [3, 1, 2, 3, 4, 5, 3] print(my_list) remove_it(3, my_list) print(my_list)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!