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
- ChainMap from Collection
- Manipulate and Merge Multi Lists using Sorted, ItemGetter and ChainMap
- Full Python scripts of merging Muti 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 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.
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“.
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
mergedLists = groupby(sorted(listA+ listB, key=itemgetter("ID")), itemgetter("ID"))
result = [dict(ChainMap(*g)) for k, g in mergedLists]
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.
- Support and Donate to our channel through PayPal (paypal.me/Easy2digital)
- Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
- Follow and like my page Easy2Digital Facebook page
- Share the article on your social network with the hashtag #easy2digital
- You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount codes
- Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)