How To Merge Two Lists in Python

Use list concatenation or the splat operator

Jonathan Hsu
Dec 23, 2019 · 2 min read
Image for post
Image for post
Photo by John Mark Arnold on Unsplash

When you want to take two data sets, a pair of lists in our case, merging them into a single sequence is a common task. It’s also a task that can be performed in many different ways, depending on circumstances.

We’re going to demonstrate two separate ways to take two existing lists and create a third that is the combination of present values.

Using List Concatenation

The first method is to concatenate the two lists together. The term concatenate means to link or chain things together. We often see this in strings.

For example, concatenating a first and last name together with a space in between: first_name + " " + last_name. In this example, the plus sign is our concatenation operator.

While not true in all languages, in Python, the plus sign also enables lists to be concatenated.

a = [1,2,3,4,5]
b = [6,7,8,9,0]
c = a + b
print(c) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Using the Splat Operator

An alternative to concatenation is to use the splat operator * within a list comprehension.

You may have seen the asterisk used in *args or **kwargs— those aren’t special Python variables, they are standard-named variables with the splat operator.

This operator will “unpack” a complex data type such as a list or dictionary. To achieve our merged list, the lists to be merged will be “splatted” within a list comprehension.

a = [1,2,3,4,5]
b = [6,7,8,9,0]
c = [*a, *b]
print(c) # a = [starter,2,3,4,5]
b = [6,7,8,9,0]
c = [*a, *b]
print(c) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

So, Which Is Better?

Well, that depends. In terms of simplicity of code, the concatenation operator reigns supreme. It doesn’t get any more clear than a single operator that is also common among other data types.

However, for a small amount of additional syntax, using list comprehension as a high-level requirement and, as a result, the splat operator, will yield a significantly higher amount of flexibility and control.

For example, what if we also have a literal we’d like to add to our merged list?

a = [1,2,3]
b = [5,6,7]
c = a + 4 + b
print(c) # TypeError: can only concatenate list (not "int") to list

We cannot easily add to our merge. In fact, the literal 4 would need to be wrapped as a list for this technique to work.

Conversely, with list comprehension and the splat operator, we have no trouble.

a = [1,2,3]
b = [5,6,7]
c = [*a, 4, *b]
print(c) # [1, 2, 3, 4, 5, 6, 7]

Which technique do you prefer and why? Join the conversation below — leave your comments and feedback!

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store