0

I am a Network Engineer trying to learn Python programming as a job requirement.

I wrote this code below to

# Funtion to chop the first and last item in the list

def chop(t):
    t.pop(0) and t.pop(len(t)-1)
    return t

When I run the function on the list t and assign it to a variable a. a gets the remainder of the list after the function was executed and a becomes a new list.This works perfect.

>>> t = ['a', 'b', 'c', 'd', 'e' ,'f','g','h','i','j','k','l']
>>> a=chop(t)
>>> a
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
>>> 

Later when i try it works well but the value of a also changes to the output of print chop(t) whereas I did not run the variable a through the function chop(t). Can someone explain why does this happen?

>>> print chop(t)
['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
>>> a
['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

Regards Umesh

  • 3
    You're not copying your list, so `a` and `t` refer to the same list. If you alter one, they are both being altered. – khelwood Mar 28 '17 at 10:28
  • You may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. Also see [Other languages have "variables", Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables) for a brief summary with nice diagrams. – PM 2Ring Mar 28 '17 at 10:29
  • You should look at this SO [article](http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – latsha Mar 28 '17 at 10:30
  • To add to what khelwood has said, the `chop` function mutates the list object that you pass to it, and returns that same list object. So the assignment `a = chop(t)` makes `a` another name for the same list object that is also named `t`, just as if you'd done `a = t`. – PM 2Ring Mar 28 '17 at 10:32
  • If you want to create a _new_ list object instead of mutating the existing one, you can just do `t[1:-1]` – PM 2Ring Mar 28 '17 at 10:34

3 Answers3

1

To save unmodified list t, create copy of it by a = t[:], and test chop on list a

>>> t = ['a', 'b', 'c', 'd', 'e' ,'f','g','h','i','j','k','l']
>>> a=t[:]
>>> a.pop(0)
'a'
>>> a
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
>>> t
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
>>> 
darvark
  • 314
  • 3
  • 15
  • Rather than doing `a=t[:]; a.pop(0)` it's more efficient to do `a=t[1:]`. When you pop from the start of a list all the subsequent list items have to be moved down. That happens at C speed, so it's relatively fast, but it's better to avoid it if you can. – PM 2Ring Mar 28 '17 at 11:19
0

When you call chop(t) , t is being passed by reference. So when you do the operation "t.pop(0) and t.pop(len(t)-1)" , it is being done on the original object and not a copy.

Because of the above , the below is true as well

>>> t = ['a', 'b', 'c', 'd', 'e' ,'f','g','h','i','j','k','l']
>>> chop(t)
>>> t
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
>>> 
codemonkey
  • 700
  • 6
  • 12
0
t = ['a', 'b', 'c', 'd', 'e' ,'f','g','h','i','j','k','l']
def chop(t):
    t.pop(0) and t.pop(len(t)-1)
    return t
a=chop(t)

In python, Assignment never copies data. By doing a=chop(t) is like Simply storing the reference to the function in a variable. Hence, every time we call a we get chop() function executed.

Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34