Python: understanding the difference between adding and expanding

The code below will not work in the current state. However, if I change sum_vec.extend( vec1[i] + vec2[i] ) to sum_vec.append( vec1[i] + vec2[i] ) , it works fine. I understand the main differences between add and extension, but I do not understand why the code does not work if I use the extension.

 def addVectors(v1, v2): vec1 = list(v1) vec2 = list(v2) sum_vec = [] vec1_len = len(vec1) vec2_len = len(vec2) min_len = min( vec1_len, vec2_len ) # adding up elements pointwise if vec1_len == 0 and vec2_len == 0: return sum_vec else: for i in xrange(0, min_len): sum_vec.extend( vec1[i] + vec2[i] ) # in case one vector is longer than the other if vec1_len != vec2_len: if vec1_len > vec2_len: sum_vec.extend( vec1[min_len : vec1_len] ) else: sum_vec.extend( vec2[min_len : vec2_len] ) print sum_vec return sum_vec v1 = [1,3,5] v2 = [2,4,6,8,10] addVectors(v1,v2) 
+7
python list append extend
source share
5 answers

As others have noted, extend iterates (for example, a list, tuple or string) and adds each iterable item to the list one at a time, and append adds its argument to the end of the list as a separate item. The main thing to note is that extend is a more efficient version of calling append several times.

 a = [1,2] b = [1,2] a.extend([3, 4]) for x in [3, 4]: b.append(x) assert a == b 

append may take an iterative argument, but it treats it as a single object:

 a = [1,2] a.append([3,4]) assert a == [1, 2, [3, 4]] # not [1, 2, 3, 4] 
+17
source share

You can read the documentation on list :

list.append adds one element to the end of your list:

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

list.extend uses list.extend and adds all its elements to the end of your list:

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

You need to use:

 sum_vec.extend([vec1[i] + vec2[i]]) # note that a list is created 

Thus, iterability with one element is transmitted ( vec1[i] + vec2[i] ). But list.append more suitable when you always add one item.

+5
source share

When you run your code, you get the same exception:

 Traceback (most recent call last): File ".../stack.py", line 28, in <module> addVectors(v1,v2) File ".../stack.py", line 15, in addVectors sum_vec.extend( vec1[i] + vec2[i] ) TypeError: 'int' object is not iterable 

In other words, the extend method expects an iterative argument. However, the append method receives the element as an argument.

Here is a small example of the difference between extension and addition:

 l = [1, 2, 3, 4] m = [10, 11] r = list(m) m.append(l) r.extend(l) print(m) print(r) 

Output:

 [10, 11, [1, 2, 3, 4]] [10, 11, 1, 2, 3, 4] 
+5
source share

The append method adds its parameter as one element to the list, and extend receives the list and adds its contents.

 letters = ['a', 'b'] letters.extend(['c', 'd']) print(letters) # ['a', 'b', 'c', 'd'] letters.append(['e', 'f']) print(letters) # ['a', 'b', 'c', 'd', ['e', 'f']] names = ['Foo', 'Bar'] names.append('Baz') print(names) # ['Foo', 'Bar', 'Baz'] names.extend('Moo') print(names) # ['Foo', 'Bar', 'Baz', 'M', 'o', 'o'] 
+2
source share

extend requires iteration as a parameter if you want to expand the list with one element that you need to put on, is in the list

 a = [] b = 1 a.extend([b]) a.append(b) 
+1
source share

All Articles