-1

I was adding and removing elements to an array reference within a method, and i found that though the elements were getting added to the array in reference but it was not getting removed.

def check(arr):
    arr.append(1)
    arr = arr[:-1]

arr = [1]
check(arr)
print(arr)

gives the output [1, 1] I want to know why arr = arr[:-1] not removing from the referenced array

EDIT: a lot of people are posting the correct solution, I am not looking for a solution, instead an explanation of why and how python creates a local variable instead of overwriting the global scope and how does it maintain the two by the same name !

3
  • arr = arr[:-1] This creates a new local variable named arr. It does not affect the original argument. Commented Jan 15, 2022 at 16:15
  • arr[:] = arr[:-1] or (for this particular case) del arr[-1] Commented Jan 15, 2022 at 16:18
  • You don't have an array but a list. Those two are different. Commented Jan 15, 2022 at 16:23

3 Answers 3

3

As I've already pointed in this comment, if you need in-place list modification, you can apply slice notation:

def check(arr):
    arr.append(1)
    arr[:] = arr[:-1]

But in fact this code will just remove last item (which you have added one line above), so you can just use del:

def check(arr):
    arr.append(1)
    del arr[-1]
Sign up to request clarification or add additional context in comments.

3 Comments

As I have commented on the other answer, can you give an explanation on how python creates a local copy instead of overwriting the global value, how can the local and global be accessed differently ? is this is what is known as the mutable and immutable in python ?
@isnvi23h4, most of information you can find in official tutorial: 9.2. Python Scopes and Namespaces. Long story short, when you pass list as argument to function, you pass a variable which contains a reference to this list, so changing value of this variable to another reference does not affect content of list which have been originally passed. But when you're using slice notation, it works with list object itself, so modification happens.
2

Alternatively it can be done with a global variable:

def check_arr():
    global arr
    arr.append(1)
    arr = arr[:-1]

arr = [1]
check_arr()
print(arr)

2 Comments

Although the solution would work, using the global keyword in python should be avoided to keep the global scope as clean as possible. python.org/dev/peps/pep-0008/#global-variable-names or stackoverflow.com/questions/19158339/….
@Wrench56 you're right. It's just an example. For educational purposes rather.
0

You don't return anything. The arr in the function is in local scope, so the global scope arr won't update

def check(arr):
    arr.append(1)
    return arr[:-1]

arr = [1]
arr = check(arr)
print(arr)

1 Comment

correct: the func param arr is shadowing the global var arr in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.