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

5
  • 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. Commented Mar 28, 2017 at 10:28
  • You may find this article helpful: Facts and myths about Python names and values, which was written by SO veteran Ned Batchelder. Also see Other languages have "variables", Python has "names" for a brief summary with nice diagrams. Commented Mar 28, 2017 at 10:29
  • You should look at this SO article Commented Mar 28, 2017 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. Commented Mar 28, 2017 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] Commented Mar 28, 2017 at 10:34

3 Answers 3

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']
>>> 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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']
>>> 

Comments

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.

Comments

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.