Python append () vs. + operator in lists, why do they give different results?

Why do these two operations ( append() and + ) give different results?

 >>> c = [1, 2, 3] >>> c [1, 2, 3] >>> c += c >>> c [1, 2, 3, 1, 2, 3] >>> c = [1, 2, 3] >>> c.append(c) >>> c [1, 2, 3, [...]] >>> 

In the latter case, there is actually infinite recursion. c[-1] and c same. Why is this different from operation + ?

+66
python list append nested-lists
Jan 07
source share
7 answers

To explain why:

Operation + adds an array of elements to the original array. The array.append operation inserts an array (or any object) at the end of the original array, which leads to a reference to self in this place (hence, infinite recursion).

The difference here is that the + operation acts when you add an array (it is overloaded, like the others, see this chapter on sequences) concatenating an element. However, the append method does literally what you ask for: add the object on the right side that you give it (an array or any other object), instead of accepting its elements.

Alternative

Use extend() if you want to use a function that acts similarly to the + operator (as others show here). It is not good to do the opposite: try to imitate the application using the + operator for lists (see Link).

Little story

Fun little story: the birth of an array module in Python in February 1993. This may surprise you, but arrays were added after the appearance of sequences and lists.

+100
Jan 07
source share

append adds an item to the list. if you want to expand the list with the new list, you need to use extend .

 >>> c = [1, 2, 3] >>> c.extend(c) >>> c [1, 2, 3, 1, 2, 3] 
+15
Jan 07 '10 at 16:59
source share

The concatenation operator + is a binary infix operator that, when applied to lists, returns a new list containing all the elements of each of the two operands. The list.append() method is a mutator on list that adds its only argument object (in your specific example, list c ) to the list topic. In your example, this leads to c adding a reference to itself (hence the infinite recursion).

The "+" concatenation alternative

The list.extend() method is also a mutator method that combines its sequence argument with a list object. In particular, it adds each of the sequence elements to the iteration order.

Aside

Being an operator, + returns the result of the expression as a new value. Being a non-targeted mutator method, list.extend() modifies a list of objects in place and returns nothing.

Arrays

I added this because of the potential confusion that Abel's answer can answer above, mixing up the discussion of lists, sequences, and arrays. Arrays were added in Python after sequences and lists as a more efficient way to store arrays of integral data types. Do not confuse Arrays with lists . They are not the same.

From the docs array :

Arrays are types of sequences and are very similar to lists, except that the type of objects stored in them is limited. The type is specified at the time the object was created using the type code, which is the only character.

+8
Jul 03 '13 at 3:35
source share

Python lists are heterogeneous, and elements in a single list can be any type of object. Expression: c.append(c) adds the object c to the fact that it can be in the list. In the case when he makes the list himself a member of the list.

The expression c += c combines the two lists and assigns the result to the variable c . The overloaded + operator is defined in lists to create a new list, the contents of which are elements in the first list and elements in the second list.

So these are really just different expressions used to create different things in design.

+7
Jan 07 '10 at 17:00
source share

The method you are looking for is extend() . From the Python documentation:

 list.append(x) Add an item to the end of the list; equivalent to a[len(a):] = [x]. list.extend(L) Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L. list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x). 
+5
Jan 07 '10 at 17:03
source share

you should use extend ()

 >>> c=[1,2,3] >>> c.extend(c) >>> c [1, 2, 3, 1, 2, 3] 

other information: add and extend

+1
Jan 07 '10 at 17:02
source share

See the documentation :

list.append (x)

  • Add an item to the end of the list; is equivalent to [len (a):] = [x].

list.extend (L), - Expand the list by adding all the elements to this list; equivalent to [len (a):] = L.

c.append(c) β€œattaches” c to itself as an element. Since the list is a reference type, this creates a recursive data structure.

c += c equivalent to extend(c) , which adds elements c to c.

+1
Jan 07 '10 at 17:10
source share



All Articles