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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:53:47+00:00 2026-06-16T13:53:47+00:00

Could someone please offer a concise explanation for the difference between these two Python

  • 0

Could someone please offer a concise explanation for the difference between these two Python operations in terms of modifying the list?

demo = ["a", "b", "c"]

for d in demo:
    d = ""

print demo
#output: ['a', 'b', 'c']

for c in range(len(demo)):
    demo[c] = ""

print demo
#output: ['', '', '']

In other words, why doesn’t the first iteration modify the list? Thanks!

  • 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-16T13:53:48+00:00Added an answer on June 16, 2026 at 1:53 pm

    The loop variable d is always a reference to an element of the iterable object. The question is not really a matter of when or when isn’t it a reference. It is about the assignment operation that you are performing with the loop.

    In the first example, you are rebinding the original reference of an element in the object, with another reference to an empty string. This means you don’t actually do anything to the value. You just assign a new reference to the symbol.

    In the second example, you are performing an indexing operation and assigning a new reference to the value at that index. demo remains the same reference, and you are replacing a value in the container.
    The assignment is really the equivalent of: demo.__setitem__(c, "")

    a = 'foo'
    id(a)  # 4313267976
    a = 'bar'
    id(a)  # 4313268016
    
    l = ['foo']
    id(l)  # 4328132552
    l[0] = 'bar'
    id(l)  # 4328132552
    

    Notice how in the first example, the object id has changed. It is a reference to a new object. In the second one, we index into the list and replace a value in the container, yet the list remains the same object.

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

Sidebar

Related Questions

Could someone please help me with the following problem: There are two classes MainForm
Could someone please tell me the differences between using a Form, Panel or a
I was hoping someone could offer me a little help here please. I have
Could someone please provide explanation how Java multi-threaded program (e.g. Tomcat servlet container) is
Could someone please give a complete list of special characters that should be escaped?
I was hoping someone could offer me a little help please? I have the
could someone please explain to me what these various environment functions do specifically? ie
Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb
Could someone please suggest why this is happening... I’ve got some code to pretty
Could someone please explain why this is happening? var y = new int[]{1,2}; Console.WriteLine(y

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.