M_S_N
M_S_N

Reputation: 2810

Why list can not be concatenated with str using '+' operator in python?

I want to make sense of difference between list +='a' and list=list+'a' For example, if I do this:

li=[]
for i in 'string':
    li+=i

I get no error but if I use the second option which by convention should be the same but gives error

li=[]
for i in 'string':
    li=li+i

TypeError: can only concatenate list (not "str") to list

Correct way to do this, would be something like this:

li=[]
for i in 'string':
    li=li+[i]

I want to know how += operator is working? How it is able to get string inside the list, is it converting string to list?

Upvotes: 4

Views: 451

Answers (2)

kederrac
kederrac

Reputation: 17322

I want to know how += operator is working?

the in-place assignment += operator works in 2 ways (from the docs):

1)For immutable objects

For immutable targets such as strings, numbers, and tuples, the updated value is computed, but not assigned back to the input variable

this means that you will create a new object each time += is used (if your object is immutable)

2)For mutable objects

For mutable targets such as lists and dictionaries, the in-place method will perform the update, so no subsequent assignment is necessary

this means that you will not create a new object each time += is used (if your object is mutable)


In your example, to use li+=i you have to make sure that i is a list, otherwise will not work

Here:

li = []
for i in 'string':
    li = li + [i]

you are concatenating 2 lists but each time you are creating a new list li, you can check by using the built-in funtion id:

li=[]
for i in '1':
    print(id(li))
    li=li+[i]
    print(id(li))

output:

140661631713104
140661644452240

you could use the in-place assignment += operator like this:

li=[]
for i in '1':
    print(id(li))
    li += [i]
    print(id(li))

output:

140661641698592
140661641698592

as you can see the elements are appended to the li list without creating a new li list which is more efficient, this works identically like list.extend

to add another element to your list, not using a list, you can use list.append

Upvotes: 2

Cihat
Cihat

Reputation: 835

Python docs says:

To see why this happens, you need to know that (a) if an object implements an __iadd__ magic method, it gets called when the += augmented assignment is executed, and its return value is what gets used in the assignment statement; and (b) for lists, __iadd__ is equivalent to calling extend on the list and returning the list. That’s why we say that for lists, += is a “shorthand” for list.extend.

Since list.extend expects an iterator and a string is iterable, += works in your given example. That means, you don't even need the for-loop as list.extend will iterate through the string and add the elements to the list.
li=[] li += "string" Output: ['s', 't', 'r', 'i', 'n', 'g']

List can only concatenate (+) list.

Source:
https://docs.python.org/3/tutorial/datastructures.html https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types https://docs.python.org/3/faq/programming.html#faq-augmented-assignment-tuple-error

Upvotes: 1

Related Questions