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 Multi Lists into A Single Standalone List Using ChainMap, ItemGetter and Sorted
- ItemGetter and Sorted
- ChainMap from Collection
- Manipulate and Merge Multi Lists using Sorted, ItemGetter and ChainMap
- Full Python scripts of merging Multi Lists into A Single Standalone List Using ChainMap, ItemGetter and Sort
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 implementations. 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\n# Initializing list of dictionaries\nlist = [{"name": "Andy", "job": software engineer},\n {"name": "Judy", "job": AI engineer},\n {"name": "Louis", "age": automation engineer}]\n\n# using sorted and itemgetter to print list sorted by job\nprint "The list printed sorting by age: "\nprint sorted(list, key=itemgetter('job'))
ChaimMap from Collection
Python contains a container called “ChainMap” which encapsulates many dictionaries into one unit. ChainMap is a member of module “collections“.
Sample:
from collections import ChainMap \nd1 = {'a': 1, 'b': 2} \nd2 = {'c': 3, 'd': 4} \nd3 = {'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"))\nresult = [dict(ChainMap(*g)) for k, g in mergedLists]
Just kindly remind me that it's okay going for as long as each dictionary in each list has one same key value, and it doesn't matter if the surplus of key values are different.
Full Python scripts of merging Multi Lists into A Single Standalone List Using ChainMap, ItemGetter and Sort
If you are interested in full scripts of Manipulate and Merge Multi 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.
- If you are interested in getting the full version of the script and vibe coding prompt mentioned in this article, please subscribe to our Youtube Channel and comment “chainmap” in the following video (ChainMap Script and Vibe Coding Prompt)
- Support our channel through PayPal (paypal.me/Easy2digital)
- Follow our Facebook page
- Sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software