Can I not change a list inside a function?












1















# left rotate using slicing
def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]
print(arr)

arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
print(arr)


Result:



[3, 4, 5, 6, 7, 1, 2]
[1, 2, 3, 4, 5, 6, 7]


When I print the array outside the function it is not rotated anymore and remains how it originally was. Can someone help me understand this?










share|improve this question




















  • 3





    yes you can, but you aren't modifying the list in your function, you simply assign to a local variable arr and then the function terminates

    – juanpa.arrivillaga
    Nov 19 '18 at 15:42


















1















# left rotate using slicing
def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]
print(arr)

arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
print(arr)


Result:



[3, 4, 5, 6, 7, 1, 2]
[1, 2, 3, 4, 5, 6, 7]


When I print the array outside the function it is not rotated anymore and remains how it originally was. Can someone help me understand this?










share|improve this question




















  • 3





    yes you can, but you aren't modifying the list in your function, you simply assign to a local variable arr and then the function terminates

    – juanpa.arrivillaga
    Nov 19 '18 at 15:42
















1












1








1








# left rotate using slicing
def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]
print(arr)

arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
print(arr)


Result:



[3, 4, 5, 6, 7, 1, 2]
[1, 2, 3, 4, 5, 6, 7]


When I print the array outside the function it is not rotated anymore and remains how it originally was. Can someone help me understand this?










share|improve this question
















# left rotate using slicing
def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]
print(arr)

arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
print(arr)


Result:



[3, 4, 5, 6, 7, 1, 2]
[1, 2, 3, 4, 5, 6, 7]


When I print the array outside the function it is not rotated anymore and remains how it originally was. Can someone help me understand this?







python list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 23:43









wim

160k50305437




160k50305437










asked Nov 19 '18 at 15:33









PyRookiePyRookie

2513




2513








  • 3





    yes you can, but you aren't modifying the list in your function, you simply assign to a local variable arr and then the function terminates

    – juanpa.arrivillaga
    Nov 19 '18 at 15:42
















  • 3





    yes you can, but you aren't modifying the list in your function, you simply assign to a local variable arr and then the function terminates

    – juanpa.arrivillaga
    Nov 19 '18 at 15:42










3




3





yes you can, but you aren't modifying the list in your function, you simply assign to a local variable arr and then the function terminates

– juanpa.arrivillaga
Nov 19 '18 at 15:42







yes you can, but you aren't modifying the list in your function, you simply assign to a local variable arr and then the function terminates

– juanpa.arrivillaga
Nov 19 '18 at 15:42














4 Answers
4






active

oldest

votes


















6














Yes, you can change a list from within a function, but you need to use the correct syntax. As you've seen already, this is not the correct way:



def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]


I will try to explain why this did not work, and hope to give you a better intuition about what really happens. Inside the scope of the function shown above, there are 3 local variables: arr, k, and n. The right-hand side operations arr[k:] + arr[:k] creates a new list object, without modifying the original list, and this resulting object is bound to the local variable name arr. This does not modify the original object, because such assignment statements in Python are never mutating objects. They will only bind a name in a namespace. Think of it as if you're taking the nametag "arr" off of the old list object, which was passed in as argument, and sticking it on the new list object which was just created. The old list object is not modified by such an operation, only the local namespace is modified - the old list object becomes "anonymous" and is no longer reachable in this scope.



The solution is to use a different kind of assignment statement, a slice assignment, which does mutate:



def leftRotate(arr, k, n):
arr[:] = arr[k:] + arr[:k]


As a final note, there is a list-like data structure in stdlib which provides more efficient rotation operations (at the cost of less-efficient indexing into the middle of the collection). If you're interested in this, read the docs on the collections.deque.






share|improve this answer


























  • Thats is very helpful. Thank you

    – PyRookie
    Nov 23 '18 at 0:51



















2














The problem is list slicing is not being applied in place. Effectively a new list is created and assigned to a variable arr scoped to leftRotate, i.e. it can can be accessed within your function only. A method which does work in place will work as expected:



def rev_sort(arr, k, n):
arr.sort(reverse=True)
print(arr)

arr = [1, 2, 3, 4, 5, 6, 7]
rev_sort(arr, 2, 7)

print(arr)

[7, 6, 5, 4, 3, 2, 1]
[7, 6, 5, 4, 3, 2, 1]


In your example, you can have your function return a list and assign it to arr:



def leftRotate(arr, k, n):
arr = arr[k:]+arr[:k]
print(arr)
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print(arr)

[3, 4, 5, 6, 7, 1, 2]
[3, 4, 5, 6, 7, 1, 2]





share|improve this answer



















  • 1





    Slice assignment will modify arr in place, without requiring the return+assignment.

    – PaulMcG
    Nov 19 '18 at 20:48











  • @PaulMcG, Indeed, that's true. As per wim's solution (which is a great alternative if OP doesn't want to return arr).

    – jpp
    Nov 19 '18 at 21:30






  • 1





    That's helpful. Thank you for explaining.

    – PyRookie
    Nov 23 '18 at 0:53



















2














Your problem is because you can't change a variable inside a python function because of the scope. Read this for more info.



But resuming, you need to either return arr and assign it outside. Like this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k]
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print arr


Or if you would like, you could make arr a global. (Check this for more info on that). (Don't recommend this last one, but exists)



arr = [1, 2, 3, 4, 5, 6, 7]

#left rotate using slicing
def leftRotate( k, n):
global arr
arr=arr[k:]+arr[:k]

leftRotate( 2, 7)
print arr


Hope it helped :)






share|improve this answer


























  • Helpful. Thank you!

    – PyRookie
    Nov 23 '18 at 0:53



















-5














There are a lot of really complicated answers. Here's the "for dummies" version:




  • You are passing arr into leftRotate()

  • For nearly all purposes, you can think of this as creating another variable, also called arr which leftRotate() works on. leftRotate()'s arr is not the same as the arr you are passing in to leftRotate(). It is a copy (technically it's not a copy until you assign arr to something else, but close enough for these purposes).

  • You're not getting your modified arr back out of leftRotate() again.


You can solve this in two ways:




  • Define arr outside of leftRotate(), and don't pass arr in. I'll call this the "global" approach. Not recommended unless you have a very good reason.

  • Use return arr after your function completes. Essentially, return 'x' means leftRotate() == 'x'. In 99.99999% of cases, this is what you want.


Therefore, in your example, what you really want is this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k] # not sure this is right, but once you get the return working, it should be easy to debug
# print arr # changed
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
# leftRotate(arr, 2, 7) # changed
arr = leftRotate(arr, 2, 7)
print arr





share|improve this answer


























  • Are you sure that passing arr into leftRotate creates a copy?

    – wim
    Nov 19 '18 at 20:36






  • 2





    Your assumption that arr passed to the function is a copy is incorrect. The part that fails is that assignment to arr inside the function just assigns to a new, local name, not affecting the original arr. It is possible to update the original passed-in arr by using slice assignment, as shown in @wim 's answer.

    – PaulMcG
    Nov 19 '18 at 20:46













  • I said it was the "for dummies" version. That means it's meant to be more easily understandable, if not precisely correct. We're not all professional coders (and OP prob isn't). I can tell you it's not a pointer/assignment. "Copy" to most people is the same as "local variable outside of scope" or whatever.

    – NotAnAmbiTurner
    Nov 19 '18 at 21:06













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377924%2fcan-i-not-change-a-list-inside-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














Yes, you can change a list from within a function, but you need to use the correct syntax. As you've seen already, this is not the correct way:



def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]


I will try to explain why this did not work, and hope to give you a better intuition about what really happens. Inside the scope of the function shown above, there are 3 local variables: arr, k, and n. The right-hand side operations arr[k:] + arr[:k] creates a new list object, without modifying the original list, and this resulting object is bound to the local variable name arr. This does not modify the original object, because such assignment statements in Python are never mutating objects. They will only bind a name in a namespace. Think of it as if you're taking the nametag "arr" off of the old list object, which was passed in as argument, and sticking it on the new list object which was just created. The old list object is not modified by such an operation, only the local namespace is modified - the old list object becomes "anonymous" and is no longer reachable in this scope.



The solution is to use a different kind of assignment statement, a slice assignment, which does mutate:



def leftRotate(arr, k, n):
arr[:] = arr[k:] + arr[:k]


As a final note, there is a list-like data structure in stdlib which provides more efficient rotation operations (at the cost of less-efficient indexing into the middle of the collection). If you're interested in this, read the docs on the collections.deque.






share|improve this answer


























  • Thats is very helpful. Thank you

    – PyRookie
    Nov 23 '18 at 0:51
















6














Yes, you can change a list from within a function, but you need to use the correct syntax. As you've seen already, this is not the correct way:



def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]


I will try to explain why this did not work, and hope to give you a better intuition about what really happens. Inside the scope of the function shown above, there are 3 local variables: arr, k, and n. The right-hand side operations arr[k:] + arr[:k] creates a new list object, without modifying the original list, and this resulting object is bound to the local variable name arr. This does not modify the original object, because such assignment statements in Python are never mutating objects. They will only bind a name in a namespace. Think of it as if you're taking the nametag "arr" off of the old list object, which was passed in as argument, and sticking it on the new list object which was just created. The old list object is not modified by such an operation, only the local namespace is modified - the old list object becomes "anonymous" and is no longer reachable in this scope.



The solution is to use a different kind of assignment statement, a slice assignment, which does mutate:



def leftRotate(arr, k, n):
arr[:] = arr[k:] + arr[:k]


As a final note, there is a list-like data structure in stdlib which provides more efficient rotation operations (at the cost of less-efficient indexing into the middle of the collection). If you're interested in this, read the docs on the collections.deque.






share|improve this answer


























  • Thats is very helpful. Thank you

    – PyRookie
    Nov 23 '18 at 0:51














6












6








6







Yes, you can change a list from within a function, but you need to use the correct syntax. As you've seen already, this is not the correct way:



def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]


I will try to explain why this did not work, and hope to give you a better intuition about what really happens. Inside the scope of the function shown above, there are 3 local variables: arr, k, and n. The right-hand side operations arr[k:] + arr[:k] creates a new list object, without modifying the original list, and this resulting object is bound to the local variable name arr. This does not modify the original object, because such assignment statements in Python are never mutating objects. They will only bind a name in a namespace. Think of it as if you're taking the nametag "arr" off of the old list object, which was passed in as argument, and sticking it on the new list object which was just created. The old list object is not modified by such an operation, only the local namespace is modified - the old list object becomes "anonymous" and is no longer reachable in this scope.



The solution is to use a different kind of assignment statement, a slice assignment, which does mutate:



def leftRotate(arr, k, n):
arr[:] = arr[k:] + arr[:k]


As a final note, there is a list-like data structure in stdlib which provides more efficient rotation operations (at the cost of less-efficient indexing into the middle of the collection). If you're interested in this, read the docs on the collections.deque.






share|improve this answer















Yes, you can change a list from within a function, but you need to use the correct syntax. As you've seen already, this is not the correct way:



def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]


I will try to explain why this did not work, and hope to give you a better intuition about what really happens. Inside the scope of the function shown above, there are 3 local variables: arr, k, and n. The right-hand side operations arr[k:] + arr[:k] creates a new list object, without modifying the original list, and this resulting object is bound to the local variable name arr. This does not modify the original object, because such assignment statements in Python are never mutating objects. They will only bind a name in a namespace. Think of it as if you're taking the nametag "arr" off of the old list object, which was passed in as argument, and sticking it on the new list object which was just created. The old list object is not modified by such an operation, only the local namespace is modified - the old list object becomes "anonymous" and is no longer reachable in this scope.



The solution is to use a different kind of assignment statement, a slice assignment, which does mutate:



def leftRotate(arr, k, n):
arr[:] = arr[k:] + arr[:k]


As a final note, there is a list-like data structure in stdlib which provides more efficient rotation operations (at the cost of less-efficient indexing into the middle of the collection). If you're interested in this, read the docs on the collections.deque.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 6:22

























answered Nov 19 '18 at 15:44









wimwim

160k50305437




160k50305437













  • Thats is very helpful. Thank you

    – PyRookie
    Nov 23 '18 at 0:51



















  • Thats is very helpful. Thank you

    – PyRookie
    Nov 23 '18 at 0:51

















Thats is very helpful. Thank you

– PyRookie
Nov 23 '18 at 0:51





Thats is very helpful. Thank you

– PyRookie
Nov 23 '18 at 0:51













2














The problem is list slicing is not being applied in place. Effectively a new list is created and assigned to a variable arr scoped to leftRotate, i.e. it can can be accessed within your function only. A method which does work in place will work as expected:



def rev_sort(arr, k, n):
arr.sort(reverse=True)
print(arr)

arr = [1, 2, 3, 4, 5, 6, 7]
rev_sort(arr, 2, 7)

print(arr)

[7, 6, 5, 4, 3, 2, 1]
[7, 6, 5, 4, 3, 2, 1]


In your example, you can have your function return a list and assign it to arr:



def leftRotate(arr, k, n):
arr = arr[k:]+arr[:k]
print(arr)
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print(arr)

[3, 4, 5, 6, 7, 1, 2]
[3, 4, 5, 6, 7, 1, 2]





share|improve this answer



















  • 1





    Slice assignment will modify arr in place, without requiring the return+assignment.

    – PaulMcG
    Nov 19 '18 at 20:48











  • @PaulMcG, Indeed, that's true. As per wim's solution (which is a great alternative if OP doesn't want to return arr).

    – jpp
    Nov 19 '18 at 21:30






  • 1





    That's helpful. Thank you for explaining.

    – PyRookie
    Nov 23 '18 at 0:53
















2














The problem is list slicing is not being applied in place. Effectively a new list is created and assigned to a variable arr scoped to leftRotate, i.e. it can can be accessed within your function only. A method which does work in place will work as expected:



def rev_sort(arr, k, n):
arr.sort(reverse=True)
print(arr)

arr = [1, 2, 3, 4, 5, 6, 7]
rev_sort(arr, 2, 7)

print(arr)

[7, 6, 5, 4, 3, 2, 1]
[7, 6, 5, 4, 3, 2, 1]


In your example, you can have your function return a list and assign it to arr:



def leftRotate(arr, k, n):
arr = arr[k:]+arr[:k]
print(arr)
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print(arr)

[3, 4, 5, 6, 7, 1, 2]
[3, 4, 5, 6, 7, 1, 2]





share|improve this answer



















  • 1





    Slice assignment will modify arr in place, without requiring the return+assignment.

    – PaulMcG
    Nov 19 '18 at 20:48











  • @PaulMcG, Indeed, that's true. As per wim's solution (which is a great alternative if OP doesn't want to return arr).

    – jpp
    Nov 19 '18 at 21:30






  • 1





    That's helpful. Thank you for explaining.

    – PyRookie
    Nov 23 '18 at 0:53














2












2








2







The problem is list slicing is not being applied in place. Effectively a new list is created and assigned to a variable arr scoped to leftRotate, i.e. it can can be accessed within your function only. A method which does work in place will work as expected:



def rev_sort(arr, k, n):
arr.sort(reverse=True)
print(arr)

arr = [1, 2, 3, 4, 5, 6, 7]
rev_sort(arr, 2, 7)

print(arr)

[7, 6, 5, 4, 3, 2, 1]
[7, 6, 5, 4, 3, 2, 1]


In your example, you can have your function return a list and assign it to arr:



def leftRotate(arr, k, n):
arr = arr[k:]+arr[:k]
print(arr)
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print(arr)

[3, 4, 5, 6, 7, 1, 2]
[3, 4, 5, 6, 7, 1, 2]





share|improve this answer













The problem is list slicing is not being applied in place. Effectively a new list is created and assigned to a variable arr scoped to leftRotate, i.e. it can can be accessed within your function only. A method which does work in place will work as expected:



def rev_sort(arr, k, n):
arr.sort(reverse=True)
print(arr)

arr = [1, 2, 3, 4, 5, 6, 7]
rev_sort(arr, 2, 7)

print(arr)

[7, 6, 5, 4, 3, 2, 1]
[7, 6, 5, 4, 3, 2, 1]


In your example, you can have your function return a list and assign it to arr:



def leftRotate(arr, k, n):
arr = arr[k:]+arr[:k]
print(arr)
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print(arr)

[3, 4, 5, 6, 7, 1, 2]
[3, 4, 5, 6, 7, 1, 2]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 15:43









jppjpp

97.1k2159109




97.1k2159109








  • 1





    Slice assignment will modify arr in place, without requiring the return+assignment.

    – PaulMcG
    Nov 19 '18 at 20:48











  • @PaulMcG, Indeed, that's true. As per wim's solution (which is a great alternative if OP doesn't want to return arr).

    – jpp
    Nov 19 '18 at 21:30






  • 1





    That's helpful. Thank you for explaining.

    – PyRookie
    Nov 23 '18 at 0:53














  • 1





    Slice assignment will modify arr in place, without requiring the return+assignment.

    – PaulMcG
    Nov 19 '18 at 20:48











  • @PaulMcG, Indeed, that's true. As per wim's solution (which is a great alternative if OP doesn't want to return arr).

    – jpp
    Nov 19 '18 at 21:30






  • 1





    That's helpful. Thank you for explaining.

    – PyRookie
    Nov 23 '18 at 0:53








1




1





Slice assignment will modify arr in place, without requiring the return+assignment.

– PaulMcG
Nov 19 '18 at 20:48





Slice assignment will modify arr in place, without requiring the return+assignment.

– PaulMcG
Nov 19 '18 at 20:48













@PaulMcG, Indeed, that's true. As per wim's solution (which is a great alternative if OP doesn't want to return arr).

– jpp
Nov 19 '18 at 21:30





@PaulMcG, Indeed, that's true. As per wim's solution (which is a great alternative if OP doesn't want to return arr).

– jpp
Nov 19 '18 at 21:30




1




1





That's helpful. Thank you for explaining.

– PyRookie
Nov 23 '18 at 0:53





That's helpful. Thank you for explaining.

– PyRookie
Nov 23 '18 at 0:53











2














Your problem is because you can't change a variable inside a python function because of the scope. Read this for more info.



But resuming, you need to either return arr and assign it outside. Like this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k]
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print arr


Or if you would like, you could make arr a global. (Check this for more info on that). (Don't recommend this last one, but exists)



arr = [1, 2, 3, 4, 5, 6, 7]

#left rotate using slicing
def leftRotate( k, n):
global arr
arr=arr[k:]+arr[:k]

leftRotate( 2, 7)
print arr


Hope it helped :)






share|improve this answer


























  • Helpful. Thank you!

    – PyRookie
    Nov 23 '18 at 0:53
















2














Your problem is because you can't change a variable inside a python function because of the scope. Read this for more info.



But resuming, you need to either return arr and assign it outside. Like this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k]
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print arr


Or if you would like, you could make arr a global. (Check this for more info on that). (Don't recommend this last one, but exists)



arr = [1, 2, 3, 4, 5, 6, 7]

#left rotate using slicing
def leftRotate( k, n):
global arr
arr=arr[k:]+arr[:k]

leftRotate( 2, 7)
print arr


Hope it helped :)






share|improve this answer


























  • Helpful. Thank you!

    – PyRookie
    Nov 23 '18 at 0:53














2












2








2







Your problem is because you can't change a variable inside a python function because of the scope. Read this for more info.



But resuming, you need to either return arr and assign it outside. Like this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k]
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print arr


Or if you would like, you could make arr a global. (Check this for more info on that). (Don't recommend this last one, but exists)



arr = [1, 2, 3, 4, 5, 6, 7]

#left rotate using slicing
def leftRotate( k, n):
global arr
arr=arr[k:]+arr[:k]

leftRotate( 2, 7)
print arr


Hope it helped :)






share|improve this answer















Your problem is because you can't change a variable inside a python function because of the scope. Read this for more info.



But resuming, you need to either return arr and assign it outside. Like this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k]
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
arr = leftRotate(arr, 2, 7)
print arr


Or if you would like, you could make arr a global. (Check this for more info on that). (Don't recommend this last one, but exists)



arr = [1, 2, 3, 4, 5, 6, 7]

#left rotate using slicing
def leftRotate( k, n):
global arr
arr=arr[k:]+arr[:k]

leftRotate( 2, 7)
print arr


Hope it helped :)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 23:44

























answered Nov 19 '18 at 15:45









Marcos JotaMarcos Jota

744




744













  • Helpful. Thank you!

    – PyRookie
    Nov 23 '18 at 0:53



















  • Helpful. Thank you!

    – PyRookie
    Nov 23 '18 at 0:53

















Helpful. Thank you!

– PyRookie
Nov 23 '18 at 0:53





Helpful. Thank you!

– PyRookie
Nov 23 '18 at 0:53











-5














There are a lot of really complicated answers. Here's the "for dummies" version:




  • You are passing arr into leftRotate()

  • For nearly all purposes, you can think of this as creating another variable, also called arr which leftRotate() works on. leftRotate()'s arr is not the same as the arr you are passing in to leftRotate(). It is a copy (technically it's not a copy until you assign arr to something else, but close enough for these purposes).

  • You're not getting your modified arr back out of leftRotate() again.


You can solve this in two ways:




  • Define arr outside of leftRotate(), and don't pass arr in. I'll call this the "global" approach. Not recommended unless you have a very good reason.

  • Use return arr after your function completes. Essentially, return 'x' means leftRotate() == 'x'. In 99.99999% of cases, this is what you want.


Therefore, in your example, what you really want is this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k] # not sure this is right, but once you get the return working, it should be easy to debug
# print arr # changed
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
# leftRotate(arr, 2, 7) # changed
arr = leftRotate(arr, 2, 7)
print arr





share|improve this answer


























  • Are you sure that passing arr into leftRotate creates a copy?

    – wim
    Nov 19 '18 at 20:36






  • 2





    Your assumption that arr passed to the function is a copy is incorrect. The part that fails is that assignment to arr inside the function just assigns to a new, local name, not affecting the original arr. It is possible to update the original passed-in arr by using slice assignment, as shown in @wim 's answer.

    – PaulMcG
    Nov 19 '18 at 20:46













  • I said it was the "for dummies" version. That means it's meant to be more easily understandable, if not precisely correct. We're not all professional coders (and OP prob isn't). I can tell you it's not a pointer/assignment. "Copy" to most people is the same as "local variable outside of scope" or whatever.

    – NotAnAmbiTurner
    Nov 19 '18 at 21:06


















-5














There are a lot of really complicated answers. Here's the "for dummies" version:




  • You are passing arr into leftRotate()

  • For nearly all purposes, you can think of this as creating another variable, also called arr which leftRotate() works on. leftRotate()'s arr is not the same as the arr you are passing in to leftRotate(). It is a copy (technically it's not a copy until you assign arr to something else, but close enough for these purposes).

  • You're not getting your modified arr back out of leftRotate() again.


You can solve this in two ways:




  • Define arr outside of leftRotate(), and don't pass arr in. I'll call this the "global" approach. Not recommended unless you have a very good reason.

  • Use return arr after your function completes. Essentially, return 'x' means leftRotate() == 'x'. In 99.99999% of cases, this is what you want.


Therefore, in your example, what you really want is this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k] # not sure this is right, but once you get the return working, it should be easy to debug
# print arr # changed
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
# leftRotate(arr, 2, 7) # changed
arr = leftRotate(arr, 2, 7)
print arr





share|improve this answer


























  • Are you sure that passing arr into leftRotate creates a copy?

    – wim
    Nov 19 '18 at 20:36






  • 2





    Your assumption that arr passed to the function is a copy is incorrect. The part that fails is that assignment to arr inside the function just assigns to a new, local name, not affecting the original arr. It is possible to update the original passed-in arr by using slice assignment, as shown in @wim 's answer.

    – PaulMcG
    Nov 19 '18 at 20:46













  • I said it was the "for dummies" version. That means it's meant to be more easily understandable, if not precisely correct. We're not all professional coders (and OP prob isn't). I can tell you it's not a pointer/assignment. "Copy" to most people is the same as "local variable outside of scope" or whatever.

    – NotAnAmbiTurner
    Nov 19 '18 at 21:06
















-5












-5








-5







There are a lot of really complicated answers. Here's the "for dummies" version:




  • You are passing arr into leftRotate()

  • For nearly all purposes, you can think of this as creating another variable, also called arr which leftRotate() works on. leftRotate()'s arr is not the same as the arr you are passing in to leftRotate(). It is a copy (technically it's not a copy until you assign arr to something else, but close enough for these purposes).

  • You're not getting your modified arr back out of leftRotate() again.


You can solve this in two ways:




  • Define arr outside of leftRotate(), and don't pass arr in. I'll call this the "global" approach. Not recommended unless you have a very good reason.

  • Use return arr after your function completes. Essentially, return 'x' means leftRotate() == 'x'. In 99.99999% of cases, this is what you want.


Therefore, in your example, what you really want is this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k] # not sure this is right, but once you get the return working, it should be easy to debug
# print arr # changed
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
# leftRotate(arr, 2, 7) # changed
arr = leftRotate(arr, 2, 7)
print arr





share|improve this answer















There are a lot of really complicated answers. Here's the "for dummies" version:




  • You are passing arr into leftRotate()

  • For nearly all purposes, you can think of this as creating another variable, also called arr which leftRotate() works on. leftRotate()'s arr is not the same as the arr you are passing in to leftRotate(). It is a copy (technically it's not a copy until you assign arr to something else, but close enough for these purposes).

  • You're not getting your modified arr back out of leftRotate() again.


You can solve this in two ways:




  • Define arr outside of leftRotate(), and don't pass arr in. I'll call this the "global" approach. Not recommended unless you have a very good reason.

  • Use return arr after your function completes. Essentially, return 'x' means leftRotate() == 'x'. In 99.99999% of cases, this is what you want.


Therefore, in your example, what you really want is this:



#left rotate using slicing
def leftRotate(arr, k, n):
arr=arr[k:]+arr[:k] # not sure this is right, but once you get the return working, it should be easy to debug
# print arr # changed
return arr

arr = [1, 2, 3, 4, 5, 6, 7]
# leftRotate(arr, 2, 7) # changed
arr = leftRotate(arr, 2, 7)
print arr






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 6:53

























answered Nov 19 '18 at 16:11









NotAnAmbiTurnerNotAnAmbiTurner

743521




743521













  • Are you sure that passing arr into leftRotate creates a copy?

    – wim
    Nov 19 '18 at 20:36






  • 2





    Your assumption that arr passed to the function is a copy is incorrect. The part that fails is that assignment to arr inside the function just assigns to a new, local name, not affecting the original arr. It is possible to update the original passed-in arr by using slice assignment, as shown in @wim 's answer.

    – PaulMcG
    Nov 19 '18 at 20:46













  • I said it was the "for dummies" version. That means it's meant to be more easily understandable, if not precisely correct. We're not all professional coders (and OP prob isn't). I can tell you it's not a pointer/assignment. "Copy" to most people is the same as "local variable outside of scope" or whatever.

    – NotAnAmbiTurner
    Nov 19 '18 at 21:06





















  • Are you sure that passing arr into leftRotate creates a copy?

    – wim
    Nov 19 '18 at 20:36






  • 2





    Your assumption that arr passed to the function is a copy is incorrect. The part that fails is that assignment to arr inside the function just assigns to a new, local name, not affecting the original arr. It is possible to update the original passed-in arr by using slice assignment, as shown in @wim 's answer.

    – PaulMcG
    Nov 19 '18 at 20:46













  • I said it was the "for dummies" version. That means it's meant to be more easily understandable, if not precisely correct. We're not all professional coders (and OP prob isn't). I can tell you it's not a pointer/assignment. "Copy" to most people is the same as "local variable outside of scope" or whatever.

    – NotAnAmbiTurner
    Nov 19 '18 at 21:06



















Are you sure that passing arr into leftRotate creates a copy?

– wim
Nov 19 '18 at 20:36





Are you sure that passing arr into leftRotate creates a copy?

– wim
Nov 19 '18 at 20:36




2




2





Your assumption that arr passed to the function is a copy is incorrect. The part that fails is that assignment to arr inside the function just assigns to a new, local name, not affecting the original arr. It is possible to update the original passed-in arr by using slice assignment, as shown in @wim 's answer.

– PaulMcG
Nov 19 '18 at 20:46







Your assumption that arr passed to the function is a copy is incorrect. The part that fails is that assignment to arr inside the function just assigns to a new, local name, not affecting the original arr. It is possible to update the original passed-in arr by using slice assignment, as shown in @wim 's answer.

– PaulMcG
Nov 19 '18 at 20:46















I said it was the "for dummies" version. That means it's meant to be more easily understandable, if not precisely correct. We're not all professional coders (and OP prob isn't). I can tell you it's not a pointer/assignment. "Copy" to most people is the same as "local variable outside of scope" or whatever.

– NotAnAmbiTurner
Nov 19 '18 at 21:06







I said it was the "for dummies" version. That means it's meant to be more easily understandable, if not precisely correct. We're not all professional coders (and OP prob isn't). I can tell you it's not a pointer/assignment. "Copy" to most people is the same as "local variable outside of scope" or whatever.

– NotAnAmbiTurner
Nov 19 '18 at 21:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377924%2fcan-i-not-change-a-list-inside-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?