-2

I have been learning to program in python, and came across this question which I have been struggling to solve. The question is as follows:

Write a function f(list, start, end) which takes as arguments a list and two indices and modifies the argument list so that it is equal to the result of the slice expression list[start:end]

I can write a function that splices the list for positive indices, ie:

def f(this_list, start, end):
    this_list=this_list[start:end+1]

But how do I get it to update whatever list the function is pointed to in the global namespace?

So, for instance, if I then get it to run:

x=[1, 2, 3, 4, 5]
f(x, 2, 4)
print x

it returns the originally defined x, not the updated. So this is because it has only updated the list in the function's namespace, yes? But then how can I get it to update x globally?

leob
  • 141
  • 2
  • 7
  • 1
    Homework question? – B001ᛦ Sep 01 '16 at 11:53
  • 1
    @bub Of course it is – byxor Sep 01 '16 at 11:54
  • One I found in a set of exercises, but not assessed homework – leob Sep 01 '16 at 11:55
  • 1
    @leob It will still get the same response on StackOverflow. This website isn't appropriate for "can you write this for me" questions. – byxor Sep 01 '16 at 11:55
  • We're not going to *"provide a solution"*. Put some effort in; at least show what you've tried and be specific about how it fails (see [mcve]). – jonrsharpe Sep 01 '16 at 11:57
  • No worries, my attempt has been added, thanks! – leob Sep 01 '16 at 11:58
  • 1
    Do a search on [Slice Assignment](http://stackoverflow.com/questions/10623302/how-assignment-works-with-python-list-slice). BTW, `list` is not a good choice for a variable name because it shadows the built-in `list` type. – PM 2Ring Sep 01 '16 at 12:04
  • Not sure what they're hoping to teach you. The trick to the question is to use only operations that work in place. Assignment doesn't work in place, so you can't use it. – cbare Sep 01 '16 at 12:15
  • 1
    You should definitely read [Ned Batchelder's Fact's and Myths about Python Names and Values](http://nedbatchelder.com/text/names.html). Essentially, the problem is that assignment doesn't alter objects, it changes the objects that variables point to. Also, check out this [blog post](https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/) about Python's evaluation strategy - it is neither call by value or call by reference. It is technically known as [call by sharing](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing). – juanpa.arrivillaga Sep 01 '16 at 12:22
  • if you ___really___ need to update a variable in the global scope, use [`globals()['variable_name']= value`](https://docs.python.org/3.5/library/functions.html#globals). – Aran-Fey Sep 01 '16 at 13:02
  • @Rawing: You're kidding, right? Using the `global` keyword is better than directly man-handling the `globals()` dict. But neither of those things are needed here. – PM 2Ring Sep 01 '16 at 16:51
  • Hopefully my previous hint led you to a solution. But if it didn't, give this a go: `this_list[:] = this_list[start:end+1]` – PM 2Ring Sep 01 '16 at 16:53
  • @PM2Ring The `global` keyword does something completely different. – Aran-Fey Sep 01 '16 at 17:04

1 Answers1

0

As someone already mentioned in the comments, you should definitely read Facts and myths about Python names and values

The simple solution to your problem is making a new value and returning it. This will work :

def f(this_list, start, end):
    this_list=this_list[start:end+1]
    return this_list
Shoaib Khan
  • 88
  • 1
  • 9
  • I've just been having a read through it, it has helped remove some confusion. Is it possible to update the list globally without returning anything from the function, however? I know it probably sounds like little bit of a strange thing when returning the updated list is so simple, but simply out of interest is there some way to do it? – leob Sep 01 '16 at 12:57