Friday, November 13, 2020

Stack Abuse: How to Concatenate Two Lists in Python

Introduction

List concatenation the act of creating a single list from multiple smaller lists by daisy chaining them together.

There are many ways of concatenating lists in Python. Specifically, in this article, we'll be going over how to concatenate two lists in Python using the plus operator, unpack operator, multiply operator, manual for loop concatenation, the itertools.chain() function and the inbuilt list method extend().

In all the code snippets below, we'll make use of the following lists:

list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]

Plus Operator List Concatenation

The simplest and most straightforward way to concatenate two lists in Python is the plus (+) operator:

list_c  = list_a + list_b
print (list_c)
[1, 2, 3, 4, 5, 6, 7, 8]

Unpack Operator List Concatenation

This method allows you to join multiple lists. It is a fairly new feature and only available from Python 3.6+. The unpacking operator, as the name implies, unpacks an iterable object into its elements. Unpacking is useful when we want to generate a plethora of arguments from a single list. For example:

def foo(a, b, c, d):
    return a + b + c + d

# We want to use the arguments of the following list with the foo function.
# However, foo doesn't take a list, it takes 4 numbers, which is why we need to
# unpack the list.
foo(*list_a)
# This is the same as if we were to call foo(1,2,3,4)
10

In a nutshell, we use the list constructor ([a,b..]) and generate the elements of the new list in order by unpacking multiple lists one after another:

list_c  = [*list_a, *list_b, *list_a]
# This is the same as:
# list_c  = [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4]
print (list_c)
[1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4]

Multiply Operator List Concatenation

The multiply (*) operator is special case of list concatenation in Python. It is used to repeat a whole list multiple times (which is why it's denoted with a multiplication operator):

print(list_a * 2)
[1, 2, 3, 4, 1, 2, 3, 4]

for loop List Concatenation

In this method we will go through one list while appending each of its elements to another list one by one. When the loop is over you will have a single list with all the desired elements:

for i in list_b:
    list_a.append(i)
print (list_a)
[1, 2, 3, 4, 5, 6, 7, 8]

itertools.chain() List Concatenation

This method works with iterables. It constructs and returns an iterator that can later be used to construct the chained list (think of it as an arrow which just memorizes the order of elements in the resulting list):

# If we were to call itertools.chain() like so
iterator = itertools.chain([1, 2], [3, 4])

# Basically the iterator is an arrow which can give us the next() element in a sequence, so if we call a list() constructor with said iterable, it works like this:
list(iterator)

Under the hood, something along these lines is what happens:

# Iterator: The next element in this list is 1 
[1, 2], [3, 4]
 ^
# Iterator: The next element in this list is 2
[1, 2], [3, 4]
    ^
# Iterator: The next element in this list is 3 
[1, 2], [3, 4]
         ^
# Iterator: The next element in this list is 4 
[1, 2], [3, 4]
            ^
# So the list() call looks something like:
list([1,2,3,4])

# Keep in mind this is all pseudocode, Python doesn't give the developer direct control over the iterator

For this method, you will need to import itertools:

import itertools

list_c  = list(itertools.chain(list_a, list_b))
print (list_c)
[1, 2, 3, 4, 5, 6, 7, 8]

extend() List Concatenation

This is aa built-it function that can be used to expand a list. Here we are expanding the first list by adding elements of the second list to it:

list_a.extend(list_b)

print (list_a)
[1, 2, 3, 4, 5, 6, 7, 8]

Conclusion

In this article, we've gone over five ways to concatenate two lists in Python - using the plus operator, the unpack operator, the multiply operator, a for loop, itertools.chain() and extend().



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...