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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:37:47+00:00 2026-06-01T05:37:47+00:00

I’m a newcomer to Python but I understand that things should not be done

  • 0

I’m a newcomer to Python but I understand that things should not be done this way, so please consider the following code snippets as purely educational 🙂

I’m currently reading ‘Learning Python’ and trying to fully understand the following example:

>>> L = [1, 2, 3, 4, 5]
>>> for x in L:
...    x += 1
...
>>> L
[1, 2, 3, 4, 5]

I did not understand if this behavior was somewhat related to the immutability of the numeric types, so I’ve run the following test:

>>> L = [[1], [2], [3], [4], [5]]
>>> for x in L:
...    x += ['_']
...
>>> L
[[1, '_'], [2, '_'], [3, '_'], [4, '_'], [5, '_']]

Question: what makes the list unchanged in the first code and changed in the second ?

My intuition is that the syntax is misleading and that:

  • x += 1 for an integer really means x = x + 1 (thus assigning a new reference)
  • x += ['_'] for a list really means x.extend('_') (thus changing the list in place)
  • 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-01T05:37:48+00:00Added an answer on June 1, 2026 at 5:37 am

    Question: what makes the list unchanged in the first code and changed in the second ?

    In the first code, the list is a sequence of (immutable) integers. The loop sets x to refer to each element of the sequence in turn. x += 1 changes x to refer to a different integer that is one more than the value x previously referred to. The element in the original list is unchanged.

    In the second code, the list if a sequence of (mutable) lists. The loop sets x to refer to each element of the sequence in turn. x += ['_'] as x refers to a list, this extends the list referred to by x with ['_'].

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,

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.