Manipulate and Merge Muti Lists into A Single Standalone List Using ChainMap, ItemGetter, Sorted

List or array data type is a key component applied to many fields in Python and data science, such as machine learning, deep learning,  automation etc. In some cases, we might have multi lists stored on different locations, but in fact they share some common grounds. In this article, I would briefly walk through how to merge them into one list using 3 methods which are ChainMap, ItemGetter and Sort.

List or array data type is a key component applied to many fields in Python and data science, such as machine learning, deep learning,  automation etc. In some cases, we might have multi lists stored on different locations, but in fact they share some common grounds. In this article, I would briefly walk through how to merge them into one list using 3 methods which are ChainMap, ItemGetter and Sort.

Table of Contents: Merge Muti Lists into A Single Standalone List Using ChainMap, ItemGetter and Sorted

ItemGetter and Sorted

The Itemgetter can be used instead of the lambda function to achieve similar functionality. Outputs in the same way as sorted() and lambda, but has different internal implementation. It takes the keys of dictionaries and converts them into tuples. It reduces overhead and is faster and more efficient. The “operator” module has to be imported for its work. The code is explained below

  • Performance: itemgetter performs better than lambda functions in the context of time.
  • Concise: : itemgetter looks more concise when accessing multiple values than lambda functions.
Sample:
from operator import itemgetter
# Initializing list of dictionaries
list = [{"name": "Andy", "job": software engineer},
       {"name": "Judy", "job": AI engineer},
       {"name": "Louis", "age": automation engineer}]
# using sorted and itemgetter to print list sorted by job
print "The list printed sorting by age: "
print sorted(list, key=itemgetter('job'))

ChaimMap from Collection

Python contains a container called “ChainMap” which encapsulates many dictionaries into one unit. ChainMap is member of module “collections“.

Sample:

from collections import ChainMap  

d1 = {'a': 1, 'b': 2

d2 = {'c': 3, 'd': 4

d3 = {'e': 5, 'f': 6

Manipulate and Merge Multi Lists using Sorted, ItemGetter and ChainMap

After having sorted the data based on your criteria from multi lists, merging them into one list facilitates operation and automation in many ways. Here is the sample as follows using three methods mentioned earlier together.
mergedLists = groupby(sorted(listA+ listB, key=itemgetter("ID")), itemgetter("ID"))
result = [dict(ChainMap(*g)) for k, g in mergedLists]
Just kindly remind that it’s okay going for as long as each dictionary in each list has one same key value, and it doesn’t matter the surplus of key values are different.

Full Python scripts of merging Muti Lists into A Single Standalone List Using ChainMap, ItemGetter and Sort

If you are interested in full scripts of Manipulate and Merge Muti Lists into A Single Standalone List Using ChainMap, ItemGetter, Sorted, please subscribe to our newsletter by adding the message ‘ChainMap + Full scripts. We would send you the script when the up-to-date app script is live.

I hope you enjoy reading Manipulate and Merge Muti Lists into A Single Standalone List Using ChainMap, ItemGetter, Sorted. If you did, please support us by doing one of the things listed below, because it always helps out our channel.