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.
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]
An alternative to concatenation is to use the splat operator
* within a list comprehension.
You may have seen the asterisk used in
**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]
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!
☞ Python Tutorials for Beginners - Learn Python Online
☞ Learn Python in 12 Hours | Python Tutorial For Beginners
☞ Complete Python Tutorial for Beginners (2019)
☞ Python Programming Tutorial | Full Python Course for Beginners 2019
☞ Python Tutorial for Beginners [Full Course] 2019