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

  • Home
  • SEARCH
  • 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 8819081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:15:53+00:00 2026-06-14T05:15:53+00:00

The del statement in python, when applied to a list, does something very strange.

  • 0

The del statement in python, when applied to a list, does something very strange. It not only deletes the entry in the list in question, but also “backtracks” to delete the same element in whatever list the variable was derived from.

For instance:

>>> x
[1, 2, 3, 4, 5]
>>> z=x
>>> del z[1]
>>> z
[1, 3, 4, 5]
>>> x
[1, 3, 4, 5]

In other words, even though I apply delete to z, the deletion is also applied to x. This “backtracking” doesn’t come up with any other commands or functions, e.g.

>>> x=[1,2,3,4,5]
>>> z=x
>>> z+[6]
[1, 2, 3, 4, 5, 6]
>>> x
[1, 2, 3, 4, 5]

applying the add function to z leaves x unaffected, as it very well should.

This is causing me all sorts of headaches with the delete ‘NA’ scrip that I’m working on. Do you know what’s going on with the del statement, and how can I get around this problem so that if I set z=x and then apply del to z, x remains unchanged?

  • 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-14T05:15:55+00:00Added an answer on June 14, 2026 at 5:15 am

    z = x does not make a copy of the list. z and x now point to the same list. Any modification of that list will be visible through both names, z and x.

    With z + [6], you did not assign the result back to z. Try printing z after doing that. It will be unchanged, just like x.

    z += [6] would be necessary to assign the result back to z and it would have the same behavior as your first example.

    You can copy the list using one of these methods:

    z = x[:]
    z = list(x)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Something like: for (a,b) in kwargs.iteritems(): if not b : del kwargs[a] This code
What are the best practices and recommendations for using explicit del statement in python?
What does the '=>' in this statement signify? del = new SomeDelegate(() => SomeAction());
I have learned that python does not guarantee that __del__ is called whenever an
I have successfully increased the zIndex for edit,add,del and search options but alertmod is
I am currently using DB2 . I do not know much about load statement.
I'm new to Python and there's something that's been bothering me for quite some
I have a jquery delete statement and it's only deleting the NEWEST upload on
The HTML elements del , strike , or s may all be used for
TableA int class del null 13 null 13 null 13 12 13 null 23

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.