Question

I have a list that I want to add multiple values to, and I use append(), as below, to add 10 more digits:

>>> x = [1, 2, 3, 4]
>>> x.append(5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (10 given)

I kind of get what this means, so then I tried doing it with a list:

>>> x = [1, 2, 3, 4]
>>> x.append([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> x
[1, 2, 3, 4, [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]]

This isn't what I want though. It contains unnecessary square brackets. What I do want is this:

>>> x = [1, 2, 3, 4]
>>> x.what_I_want([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Is the first change that I made to take away the TypeError the correct change to make? Or is that why this isn't working?

Was it helpful?

Solution 2

Use extend():

>>> x = [1, 2, 3, 4]
>>> x.extend([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Instead of typing out the numbers from 5 through 15, use a range():

>>> x = [1, 2, 3, 4]
>>> x.extend(range(5, 15))
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

The first edit that you made was correct, since you can only append one item at a time, not a tuple like (5, 6, 7, 8, 9, 10, 11, 12, 13, 14)

OTHER TIPS

It's called extend:

>>> x = [1, 2, 3, 4]
>>> x.extend([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

http://docs.python.org/2/tutorial/datastructures.html#more-on-lists http://docs.python.org/2/library/stdtypes.html#mutable-sequence-types

For completeness, your other options are creating a new list as a concatenation of the two lists:

>>> x = [1, 2, 3, 4]
>>> x + [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

>>> x += [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

And assigning into the empty slice at the end:

>>> x = [1, 2, 3, 4]
>>> x[len(x):len(x)] = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Use extend though.

The list append() method takes a single argument, which is added to the list. Thus, when you pass it a list, that list becomes the new last element of the list to which it is appended. By maintaining such consistency Python allows us to conveniently create and process lists of lists and more complicated data structures. But it does mean append() is not the answer you are looking for.

The simplest answer to your problem I can think of is

x += range(15)

This will work fine in Python 2, but in Python 3 range() is a generator, so you have to use

x += list(range(15))

Lists can be added together as can tuples, and it works just like string concatenation.

There are several ways to do this in Python.

The most common and idiomatic approach is using the extend method, to extend the list in place.

x = [1, 2, 3, 4]
y = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
x.extend(y)
print(x)

The addition operator can also be used to do this, though it creates a new list.

x = [1, 2, 3, 4]
y = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
z = x + y # This creates a new list `z`
print(z)

Augmented assignment can also be used, to extend the list in place.

x = x_ = [1, 2, 3, 4]
y = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
x += y # This doesn't create a new list
assert x is x_
print(x)

A particularly esoteric approach would involve using the slice assignment. You'd only want to do this if you're looking to confuse people.

x = [1, 2, 3, 4]
y = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
x[len(x):] = y
print(x)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top