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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:11:41+00:00 2026-05-15T16:11:41+00:00

From what little I know, + op for lists only requires the 2nd operand

  • 0

From what little I know, + op for lists only requires the 2nd operand to be iterable, which “ha” clearly is.

In code:

>>> x = []
>>> x += "ha"
>>> x
['h', 'a']
>>> x = x + "ha"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
  • 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-15T16:11:43+00:00Added an answer on May 15, 2026 at 4:11 pm

    Using += with a list is like calling extend, not +.

    • You can call extend with an iterable.
    • You can only use + with another list.

    I can only guess why this decision was made, but I imagine it is for performance reasons. Calling + results in a new object being created and all items being copied, whereas extend can use free space in the existing list object saving a copy in some cases.

    Another side-effect of this decision is that if you write x += y other references to the list will see the change but if you use x = x + y then they will not. This is demonstrated below:

    >>> x = ['a','b']
    >>> y = ['c', d']
    >>> z = x
    >>> x += y
    >>> z
    ['a', 'b', 'c', 'd']
    
    >>> x = ['a','b']
    >>> y = ['c', d']
    >>> z = x
    >>> x = x + y
    >>> z
    ['a', 'b']
    

    References

    Python source code for list.

    Source code for +=:

    static PyObject *
    list_inplace_concat(PyListObject *self, PyObject *other)
    {
        PyObject *result;
    
        result = listextend(self, other);
        if (result == NULL)
            return result;
        Py_DECREF(result);
        Py_INCREF(self);
        return (PyObject *)self;
    }
    

    Source code for +:

    static PyObject *
    list_concat(PyListObject *a, PyObject *bb)
    {
        Py_ssize_t size;
        Py_ssize_t i;
        PyObject **src, **dest;
        PyListObject *np;
        if (!PyList_Check(bb)) {
            PyErr_Format(PyExc_TypeError,
                      "can only concatenate list (not \"%.200s\") to list",
                      bb->ob_type->tp_name);
            return NULL;
        }
    
        // etc ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know very little about Java but I'm attempting to run this applet from
I made this little function from code snippets around the net. It does what
I want to ask a little question. I want to know which is the
how to convert a binary(128) from little endian to big endian in SQL Server?
From what little understanding of Cassandra I have, it seems that data locality is
What is the keyboard-shortcut that expands the menu, from the little red line, and
A little help from those who’ve used Timthumb before. http://themes.amplus.gambit.ph/wp-content/themes/amplus/functions/timthumb.php?src=http://themes.amplus.gambit.ph/wp-content/uploads/2011/02/12979795614923485443_a1967ff888_b.jpg&w=860&h=300&f=5,255,0,0,0 This image should be
Ok this might sound a little vague from the title, but that's because I
I'm trying to finish a little module from a reporting app using external dlls.
When I moved to Objective C (iOS) from C++ (and little Java) I had

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.