How To Append Multiple Values To A List In One Line In Python

The .append() method only allows you to add 1 element to a list at 1 time. Therefore, you may have to use a loop or many lines to push multiple values to a list. This article shows you different ways to append multiple values to a list in one line in Python.

Ways to append multiple values to a list in one line in Python

Here are the ways to append multiple values to a list in one line in Python.

Use the .extend() function

The .extend() method works by adding all elements of a list to the end of another list. You can take advantage of this method to push many values to a list in one line.

The syntax is like this:

[list 1] = [elements]
[list 2] = [elements]
[list 1].extend([list2])

In which:

[elements] = [element 1, element 2, …, element N]

All the elements in the list [list 1] remain unchanged. The new elements from the [list 2] will be added to the end of the [list 1]. The order of the elements in the [list 1] will not change.

You can see the code sample below:

lstName = ["Andy", "William"]
print("List Name: ", lstName)
lstAdd = ["Josh", "Maria", "Kelvin"]

# Add multiple values from lstAdd to lstName in one line
lstName.extend(lstAdd)
print("List Name after changed: ", lstName)

The output will be:

List Name:  ['Andy', 'William']
List Name after changed:  ['Andy', 'William', 'Josh', 'Maria', 'Kelvin']

Use the + operator

Python features a built-in + operator. This operator gives developers permission to combine 2 separate lists together into a list. You can take advantage of this feature to append values to a list in one line.

Here is the syntax:

[list 1] = [elements]
[list 2] = [elements]
[list 1] = [list 1] + [list 2]

In which:

[elements] = [element 1, element 2, …, element N]

With the command [list 1] + [list 2], a new list will be created containing all elements of the 2 lists. All the elements of the [list 1] will stay before the elements of the [list 2] in order. The values of the new list will be assigned to the [list 1].

See the code sample below:

lstName = ["Andy", "William"]
print("List Name: ", lstName)
lstAdd = ["Josh", "Maria", "Kelvin"]

# Add multiple values from lstAdd to lstName in one line
lstName = lstName + lstAdd
print("List Name after changed: ", lstName)

The output will be:

List Name:  ['Andy', 'William']
List Name after changed:  ['Andy', 'William', 'Josh', 'Maria', 'Kelvin']

Use chain() from itertools module

Another way is to use the chain() from the itertools module. The chain() function can take all iterables and put them in a group. The return result is a single iterable.

The first thing to do is import the chain() function from the itertools module:

from itertools import chain

After that, follow this syntax:

[list 1] = [elements]
[list 2] = [elements]
[list 1] = list(chain([list 1], [list 2]))

The chain() function takes 2 parameters, which are [list 1] and [list 2]. It will group all these 2 lists together into a single iterable. You use the list() function to transform this iterable into a list and then assign it to the [list 1].

Here is the code sample:

from itertools import chain

lstName = ["Andy", "William"]
print("List Name: ", lstName)
lstAdd = ["Josh", "Maria", "Kelvin"]

# Add multiple values from lstAdd to lstName in one line
lstName = list(chain(lstName, lstAdd))
print("List Name after changed: ", lstName)

The output will be:

List Name:  ['Andy', 'William']
List Name after changed:  ['Andy', 'William', 'Josh', 'Maria', 'Kelvin']

Summary

To summarize, we have shared with you 3 ways to append multiple values to a list in one line in Python. The easiest ways are to use the .extend() method or the + operator. You can also import the itertools module to use the feature of the chain() function.

Leave a Reply

Your email address will not be published. Required fields are marked *