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 4531044
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:51:55+00:00 2026-05-21T13:51:55+00:00

I am absolutely dumbfounded by why the following is happening: Here is my code:

  • 0

I am absolutely dumbfounded by why the following is happening:

Here is my code:


def add_one(array):

    new_array = array
    length = len(array)

    for i in range(length):

        new_array[i] = new_array[i]+1


    return new_array

x = [1,2,3,4,5];
y = add_one(x)

print x

print y

Here are the results:

[2, 3, 4, 5, 6]

[2, 3, 4, 5, 6]

I do not understand why x is changed.

My speculations:
Somehow x is given as a global variable to the function add_one. I included ‘new_array = array’ so that if array was somehow the global variable x, x would not be changed. However, somehow new_array also became the global variable x when ‘new_array = array’ was executed. I wrote an alternate version of the function add_one which did not give me issues:


def add_one(array):

    new_array = []
    length = len(array)

    for i in range(length):
        new_array.append(array[i]+1)

It seems that if a local variable (that is a array) is edited by it’s index within a function, it turns global with respect to the global variable that was taken as an input for that function?

I have no idea what is going on. Any explanations would be highly appreciated.

  • 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-05-21T13:51:56+00:00Added an answer on May 21, 2026 at 1:51 pm

    Nothing is made global for obscure readons. array is still a perfectly normal local variable. But it’s a reference, as everything in Python (well, every name at least) is a reference:

    • add_one(x) passes a reference (not like a reference in e.g. C++, more like a pointer – if either of that means anything to you) to x to add_one
    • new_array = array will just copy that reference
    • No copying happening, all these names point to the same object!

    That means that calling a method on array in add_one modified the very same object x of the caller refers to – re-assigning array (array = []) will just overwrite the local copy of the reference and not affect other variables referring to the same object (after all, it’s quite possible that there is no such variable!), but member or item assignment (e.g. new_array[i] = ...) is basically a method call and will modify the object.

    Generally, the easiest way to generate a new list from an existing one with some pattern of modification applied to each element and optionally some filtering is using a list comprehension. [x + 1 for x array] in this example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I absolutely love the Code Folding feature inside Delphi 2010. However, each time start
I have absolutely no idea why I am getting a nullpointerexception here because almost
I'm absolutely stuck here and hoping someone here will have the answer. ok -
I did absolutely everything what is written in here: http://www.leonardaustin.com/technical/how-to-install-memcached-on-xampp-on-windows-7 I see now in
I'm absolutely new to printing in .NET. I would like to print a page
I'm trying to absolutely position some floated divs using this code: $(function() { $('#wrapper
I absolutely love the Source Code Ouliner power toy that I use in VS2005
I absolutely love the Keep Remote Directory Up-to-date feature in Winscp . Unfortunately, I
I absolutely need to use an IoC container for decoupling dependencies in an ever
I absolutely loved Dive Into Python when I picked up Python. In fact, tutorials

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.