Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7821927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:47:21+00:00 2026-06-02T07:47:21+00:00

Possible Duplicate: Understanding Python's call-by-object style of passing function arguments I recently came across

  • 0

Possible Duplicate:
Understanding Python's call-by-object style of passing function arguments

I recently came across this:

x = [1,2,3]

def change_1(x):
    x = x.remove(x[0])
    return x

Which results in:

>>> change_1(x)
>>> x
[2, 3]

I find this behavior surprising, as I thought that whatever goes in inside a function has no effect on outside variables. Furthermore, I constructed an example where basically do the same thing but without using remove:

x = [1,2,3]

def change_2(x):
    x = x[1:]
    return x

Which results in:

>>> change_2(x)
[2, 3] # Also the output prints out here not sure why this is
>>> x
[1, 2, 3]

And I get the result that I would expect, the function does not change x.

So it must be something remove specific that has effect. What is going on here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:47:22+00:00Added an answer on June 2, 2026 at 7:47 am

    One of the things that is confusing is that you’ve called lots of different things ‘x’. For example:

    def change_1(x):       # the x parameter is a reference to the global 'x' list
        x = x.remove(x[0]) # on the left, though, is a new 'x' that is local to the function
        return x           # the local x is returned
    
    >>> x = [1, 2, 3]
    >>> y = change_1(x)    # assign the return value to 'y'
    >>> print y
    None                   # this is None because x.remove() assigned None to the local 'x' inside the function
    >>> print x
    [2, 3]                 # but x.remove() modified the global x inside the function
    
    def change_2(x):
        x = x[1:]          # again, x on left is local, it gets a copy of the slice, but the 'x' parameter is not changed
        return x           # return the slice (copy)
    
    >>> x = [1, 2, 3] 
    >>> y = change_2(x)
    >>> print x
    [1, 2, 3]             # the global 'x' is not changed!
    >>> print y
    [2, 3]                # but the slice created in the function is assigned to 'y'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Understanding when to call retain on an object? I have a hard
Possible Duplicate: What does *args and **kwargs mean? Understanding kwargs in Python I've found
Possible Duplicate: Understanding Python decorators I was reading a django app source code where
Possible Duplicate: Understanding Python super() Class B subclasses class A , so in B's
Possible Duplicate: Understanding PHP's & operator I was just looking at array_filter() function doc
Possible Duplicate: Understanding which constructor is chosen and why Why compiler acts like this,
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: Understanding NSString comparison in Objective-C Was just reading up about equality vs
Possible Duplicate: Understanding Threads I have a public class that is implementing a Runnable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.