Can I used the append() function to add more than one item?


#1

Question

Is it possible to use the append() function to add more than one item at a time to a list?

Answer

No, the append() function in Python can only add one item at a time to a list. In order to add more than one item, you have to call the append() function multiple times.

# Add additional numbers to the list
primes = [2, 3]
primes.append(5)
primes.append(7)
primes.append(11)

#2
>>> c = []
>>> c.append(1,2)
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    c.append(1,2)
TypeError: append() takes exactly one argument (2 given)
>>> 

We do have another list method to help prevent repetition, .extend().

>>> primes = [2, 3]
>>> primes.extend([5, 7, 11])
>>> primes
[2, 3, 5, 7, 11]
>>> 

Note that this is equivalent to concatenating two lists.