454

I figured out how to append multiple values to a list in Python; I can manually input the values, or put the append operation in a for loop, or the append and extend functions.

Any neater ways? Maybe a package or function?

2

6 Answers 6

842

You can use the sequence method list.extend to extend the list by multiple values from any kind of iterable, being it another list or any other thing that provides a sequence of values.

>>> lst = [1, 2]
>>> lst.append(3)
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]

>>> lst.extend([5, 6, 7])
>>> lst.extend((8, 9, 10))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

So you can use list.append() to append a single value, and list.extend() to append multiple values.

Sign up to request clarification or add additional context in comments.

6 Comments

A bit complicated to introduce generators to someone on day 1 of using Python.
@DanielRoseman My point is that the values can come from any kind of iterable sequence.
Note that extend do not keep the structure of the element you add compared to append(). With your example using append, you would get [1, 2, 3, 4, [5, 6, 7], (8, 9, 10), range(11, 14)]. Python 3
@YohanObadia If that was the case for Python 3 in '17, it is no longer the case - Python 3.7.7 does exactly what poke described.
@YohanObadia That makes no sense. Append appends ONE ELEMENT, thus it "keeps the structure". But that's a duh, really. And @ Post169, nothing change between '17 and today, extend and append still behave the same way, Yohan just didn't understand what's what.
|
63

Other than the append function, if by "multiple values" you mean another list, you can simply concatenate them like so.

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a + b
[1, 2, 3, 4, 5, 6]

2 Comments

I know this is old, and at the time of writing, 45 people have agreed your answer is good, but strictly speaking the OP asked to add items to a list, and this is creating a new list. Although a very similar/related statement would be: a += b. This would append and be unique from the primary answer above.
Given a = [1, 2, 3], you can also append multiple elements as a += [4, 5, 6]. This sets a to [1, 2, 3, 4, 5, 6]. This does not create a new list; each element is appended to a. (Test this with a = [1, 2, 3]; b = a; a += [4, 5, 6] and you'll see that a and b are still the same.)
16

If you take a look at the official docs, you'll see right below append, extend. That's what your looking for.

There's also itertools.chain if you are more interested in efficient iteration than ending up with a fully populated data structure.

Comments

7

Another solution is to unpack both lists inside a new list and assign it back to the copy of the original list:

my_list[:] = [*my_list, *new_items]

An example:

my_list = [1, 2]
new_items = [3, 4, 5]

my_list[:] = [*my_list, *new_items]
my_list                                # [1, 2, 3, 4, 5]

or assign to an empty slice as in Saumya Prasad's answer (but at the end of the list):

length = len(my_list)
my_list[length:length] = new_items
my_list                                # [1, 2, 3, 4, 5]

Another option is to use operator.add which is a functional wrapper for + operator and operator.iadd which is a wrapper for +=. So like:

import operator

operator.add(my_list, new_items)  # <--- concatenates the lists

It's usefulness comes in when you need a function to concatenate two lists together (so that you don't need to compose a lambda):

xs = [[1, 2]]*5
ys = [[3, 4]]*5

new_lists = list(map(operator.add, xs, ys))

or perhaps as metadata to typing.Annotated to specify what to do with new input:

import operator
from typing import Annotated

values: Annotated[list, operator.add]

It's worth noting that list.extend and slice assignment to an empty slice both modify the list in-place whereas itertools.chain or unpacking or + operator or operator.add create a new list.

2 Comments

Somehow I would have preferred list.add(1, 2, 3) to add three items.
Although 'extend' is a direct approach, your first suggestion is also a very nice looking solution. I find it quite intuitive and elegant.
1

if the number of items was saved in a variable say n. you can use list comprehension and plus sign for list expansion.

lst = ['A', 'B']
n = 1
new_lst = lst + ['flag'+str(x) for x in range(n)]
print(my_lst)

>>> ['A','B','flag0','flag1']

Comments

1

Here we are inserting a list to the existing list by creating a variable new_values.

Note that we are inserting the values in the second index, i.e. a[2]

a = [1, 2, 7, 8]

new_values = [3, 4, 5, 6]

a.insert(2, new_values)

print(a)

But here insert() method will append the values as a list.

So here goes another way of doing the same thing, but this time, we'll actually insert the values in between the items.

a = [1, 2, 7, 8]

a[2:2] = [3,4,5,6]

print(a)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.