Eight Ways to Merge Lists in python

  • 2021-10-15 10:52:44
  • OfStack

Directory 1. The most intuitive addition
2. With the help of itertools
3. Unpack with * 4. Use extend
5. Use a list derivation
6. Use heapq
7. Use magic methods
8. Use yield from

There are many (and increasingly) advanced features in the Python language that Python enthusiasts like very much. In the eyes of these people, to be able to write those advanced features that developers can't understand is a master and a great god.

But you should know that in teamwork, showing off skills is a big taboo.

Why do you say that? Let me say my own opinion:

The simpler the code and the clearer the logic, the less likely it is to make mistakes; In teamwork, your code is not only maintained by you, but it is a good character to reduce the cost of others reading/understanding the code logic Simple code, which only uses the most basic syntax sugar, complex high-level features, will have more dependencies (such as language versions)

1. The most intuitive addition

Use + to add multiple lists, you should understand, don't say much.


>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> list03 = [7,8,9]
>>>
>>> list01 + list02 + list03
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 

2. With the help of itertools

itertools has a very powerful built-in module in Python that is dedicated to manipulating iterative objects.

As described in the previous article, using the itertools.chain() Functions first concatenate iterable objects (in this case, lists) to form a larger iterable object.

Finally, you use list to convert it into a list.


>>> from functools import chain
>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> list03 = [7,8,9]
>>>
>>> list(chain(list01, list02, list03))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

3. Unpack with *

Use * to unpack the list. * and * * are often used to set variable parameters when defining functions.

Now I take it out separately for merging multiple lists.

Examples are as follows:


>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>>
>>> [*list01, *list02]
[1, 2, 3, 4, 5, 6]
>>>

4. Use extend

In dictionaries, update enables in-place updating, while in lists, extend enables self-extension of lists.


>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>>
>>> list01.extend(list02)
>>> list01
[1, 2, 3, 4, 5, 6]

5. Use a list derivation

In Python, there is a set of very Pythonnic writing methods for generating lists, sets and dictionaries.

That is list parsing, set parsing and dictionary parsing, which are usually the favorites of Python enthusiasts. So today's topic: Can list merging and list derivation be competent?

Of course, the specific example code is as follows:


>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> list03 = [7,8,9]
>>>
>>> [x for l in (list01, list02, list03) for x in l]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

6. Using heapq

heapq is a standard module of Python, which provides the implementation of heap sorting algorithm.

There is an merge method in this module, which can be used to merge multiple lists, as shown below


>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> list03 = [7,8,9]
>>>
>>> from heapq import merge
>>>
>>> list(merge(list01, list02, list03))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 

Note that heapq. merge not only merges multiple lists, but also sorts the final list after merging.


>>> list01 = [2,5,3]
>>> list02 = [1,4,6]
>>> list03 = [7,9,8]
>>> 
>>> from heapq import merge
>>> 
>>> list(merge(list01, list02, list03))
[1, 2, 4, 5, 3, 6, 7, 9, 8]
>>> 

Its effect is equivalent to the following line of code:


sorted(itertools.chain(*iterables))

If you want a list that is always ordered, think of heapq. merge in time 1, because it uses heap sorting and is very efficient. But if you don't want to get an ordered list, don't use it.

7. Use magic methods

With the magic method __add__, in fact, when we use the first method list01 + list02, the interior actually acts on the magic method __add__.

So the following two methods are actually equivalent


>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> 
>>> list01 + list02
[1, 2, 3, 4, 5, 6]
>>> 
>>> 
>>> list01.__add__(list02)
[1, 2, 3, 4, 5, 6]
>>> 

Using this magic feature, we can combine multiple lists with reduce. The example code is as follows


>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> list03 = [7,8,9]
>>>
>>> from functools import reduce
>>> reduce(list.__add__, (list01, list02, list03))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

8. Using yield from

yield from can be followed by an iterative object that iterates and returns every element in it.

Therefore, we can customize a tool function for merging lists as follows.


>>> from functools import chain
>>> list01 = [1,2,3]
>>> list02 = [4,5,6]
>>> list03 = [7,8,9]
>>>
>>> list(chain(list01, list02, list03))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
0

See here, there is no rising posture, after learning Python for so long, I didn't expect there are so many ways to merge the list. The main idea of this article is not to let you master all the seven ways to merge lists. In fact, you only need to choose the most convenient way.

However, when you work together or read other people's code, you will inevitably encounter various writing methods. At this time, you can subconsciously know that you are doing the operation of merging lists, so this article is meaningful.

The above is the python connection list of 8 methods of the details, more information about the python connection list please pay attention to other related articles on this site!


Related articles: