Please help understand why applying the outer.remove
method on inner
doe not remove the inner list elements from the outer list.
My sincere apology for having asked python - List.remove method not applicable as the function with the map builtin? wrongly, hence opening a new question.
I thought List.remove
would be a function associated to the outer
list object, hence applying it to inner
would update the outer
list via map
. However, apparently the <built-in method remove of list object at 0x7fbd8411a840>
is not updating the outer
list it is associated to.
Please help understand if this is work as designed and what is happening under the hood in the Python interpreter. If there is a way to apply <list object>.remove
via the map builtin.
import copy
n = 10
outer = list(range(1, 2*n+1))
inner = list(range(1, n+1))
print(f"outer is {outer}")
print(f"inner is {inner}\n")
_outer = copy.deepcopy(outer)
for i in inner:
_outer.remove(i)
print(f"for loop application of outer.remove(inner_element) is \n{_outer}\n")
print(f"outer.remove is {outer.remove}")
map(outer.remove, inner)
print(f"map application of outer.remove is \n{outer}")
Result
outer is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
inner is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for loop application of outer.remove(inner_element) is
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
outer.remove is <built-in method remove of list object at 0x7fbd8411a840>
map application of outer.remove is
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]