0

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]
martineau
  • 119,623
  • 25
  • 170
  • 301
mon
  • 18,789
  • 22
  • 112
  • 205
  • 2
    In Python 3.x, `map()` returns a generator - nothing happens immediately, you have to iterate the generator to get it to actually apply the specified function. Writing it as `list(map(...))` would be one way to do this (of course, this would generate a completely useless list of Nones in the process). – jasonharper Jan 26 '21 at 01:16
  • 1
    `map(outer.remove, inner)` creates a `map` object, an *iterator*. You would have to iterate over it. **But you shouldn't use map like this**. `map` is a functional programming construct, don't use it for side-effects – juanpa.arrivillaga Jan 26 '21 at 01:18
  • 1
    @jasonharper to be pedantic, `map` does not return a generator, it returns a `map` object, which is an *iterator* but not a generator – juanpa.arrivillaga Jan 26 '21 at 01:19
  • 1
    And aside from a bad mixture of imperative/functional style, `.remove` in a loop is unnecessarily inefficient, whether it be `map` or a for-loop – juanpa.arrivillaga Jan 26 '21 at 01:20

1 Answers1

2

python has several elements of functional languages like lazy evaluation, which postpones execution until the computation result is needed. When calling

map(outer.remove, inner)

you are creating a mapping object, which will not apply dirty method remove to inner list until the computation "result" is actually demanded. Like e.g. when you put it on the list (which is immediately lost from the scope)

list(map(outer.remove, inner))

imho it`s generally a bad practice to mix functional programming and functions with side effects

JVod
  • 151
  • 5