TheRealFakeNews
TheRealFakeNews

Reputation: 8143

Python: understanding difference between append and extend

The code below will not run in its current state. However, if I change sum_vec.extend( vec1[i] + vec2[i] ) to sum_vec.append( vec1[i] + vec2[i] ) it works just fine. I understand the basic different between append and extend, but I don't understand why the code doesn't work if I use extend.

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)

Upvotes: 10

Views: 25835

Answers (5)

chepner
chepner

Reputation: 530960

As others have pointed out, extend takes an iterable (such as a list, tuple or string), and adds each element of the iterable to the list one at a time, while append adds its argument to the end of the list as a single item. The key thing to note is that extend is a more efficient version of calling append multiple times.

a = [1,2]
b = [1,2]

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

assert a == b

append can take an iterable as its 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]

Upvotes: 21

Haresh Shyara
Haresh Shyara

Reputation: 1886

The method append adds its parameter as a single element to the list, while extend gets a list and adds its content.

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']

Upvotes: 2

Marek
Marek

Reputation: 1727

extend needs an iterable as a parameter if you want to extend list by a single element you need to dress is in a list

a = []
b = 1

a.extend([b])
a.append(b)

Upvotes: 1

aldeb
aldeb

Reputation: 6828

When you run your code, you get an exception like this:

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 iterable as argument. However append method gets an item as argument.

Here is a small example of the difference between extend and append:

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]

Upvotes: 5

Reut Sharabani
Reut Sharabani

Reputation: 31339

You can read the documentation on list:

list.append adds a single item to the end of your list:

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

list.extend uses an iterable and adds all of it's elements to the end of your list:

Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.

You need to use:

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

That way an iterable with a single item (vec1[i] + vec2[i]) is passed. But list.append is more suitable when you're always adding a single item.

Upvotes: 5

Related Questions