# How to Merge Two Lists and Sort it in Python

Merging two lists using list. If we want to extend any existing list by concatenating the contents of any other lists to it.

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? Leave your comments and feedback! Thank you for reading !

#Python #Programming #Data Science #Coding #python

1 Likes25.00 GEEK