Beyond Basics: Advanced Data Handling with Python's operator.itemgetter and Lambda
I am a self-taught Python aficionado, dancing in the realms of AI and ML. What started as a curious exploration soon turned into a revelation: the unsung heroes behind the AI symphony are linear algebra, probability, and statistics. Astonishingly, these mathematical wizards not only power the algorithms but also surpass human problem-solving finesse.
- Extracting First Elements from Sublists.
item1 = [['a', 1], ['b', 2], ['c', 3], ['d', 4]]
list(map(operator.itemgetter(0), item1)) # ['a', 'b', 'c', 'd']
# Using Lambda
list(map(lambda x: x[0], item1)) # ['a', 'b', 'c', 'd']
- Extracting Last Elements from Sublists
item1 = [['a', 1], ['b', 2], ['c', 3], ['d', 4]]
list(map(operator.itemgetter(-1), item1)) # [1, 2, 3, 4]
#Using Lambda
list(map(lambda x: x[-1], item1)) # [1, 2, 3, 4]
- Retrieving Values by a Specific Key from Dictionaries
item2 = [{'Name':'Alice', 'Age':25}, {'Name':'Bob', 'Age':30} , {'Name':'Charlie', 'Age':35}]
list(map(operator.itemgetter('Age'), item2)) # [25, 30, 35]
#Using Lambda
list(map(lambda x: x['Age'], item2)) # [25, 30, 35]
- Sorting a List of Lists by the Second Element
item3 = [['a', 10], ['b', 3], ['c', 5], ['d', 1]]
sorted(item3, key=operator.itemgetter(1)) # [['d', 1], ['b', 3], ['c', 5], ['a', 10]]
# Using Lambda
sorted(item3, key=lambda x: x[1]) # [['d', 1], ['b', 3], ['c', 5], ['a', 10]]
- Sorting a List of Dictionaries by a Specific Key
item4 = [{'Name':'Alice', 'Age':50}, {'Name':'Bob', 'Age':30} , {'Name':'Charlie', 'Age':60}]
sorted(item4, key=operator.itemgetter('Age')) # [{'Name': 'Bob', 'Age': 30}, {'Name': 'Alice', 'Age': 50}, {'Name': 'Charlie', 'Age': 60}]
# Using Lambda
sorted(item4, key=lambda x: x['Age']) # [{'Name': 'Bob', 'Age': 30}, {'Name': 'Alice', 'Age': 50}, {'Name': 'Charlie', 'Age': 60}]
- Finding Maximum and Minimum in a List of Lists
item5 = [['a', 10, 45], ['b', 3, 22], ['c', 5, 7], ['d', 1, 11]]
# maximum
max(item5, key=operator.itemgetter(1)) # ['a', 10, 45]
# minimum
min(item5, key=operator.itemgetter(1)) # ['d', 1, 11]
# Using Lambda
max(item5, key=lambda x :x[1]) # ['a', 10, 45]
min(item5, key=lambda x :x[1]) # ['d', 1, 11]
- Finding Maximum and Minimum in a List of Dictionaries
item6 = [{'Name':'Alice', 'Age':50}, {'Name':'Bob', 'Age':30} , {'Name':'Charlie', 'Age':60}]
# maximum
max(item6, key=operator.itemgetter('Age')) # {'Name':'Charlie', 'Age':60}
# minimum
min(item6, key=operator.itemgetter('Age')) # {'Name':'Bob', 'Age':30}
# Using Lambda
max(item6, key=lambda x :x['Age']) # {'Name':'Charlie', 'Age':60}
min(item6, key=lambda x :x['Age']) # {'Name':'Bob', 'Age':30}
- Slicing a List with
itemgetterand Without
item7 = [2, 7, 10, 5, 11, 20, 1]
operator.itemgetter(slice(2, 5))(item7) # [10, 5, 11]
# Without itemgetter
item7[slice(2, 5)] # [10, 5, 11]
- Extracting First and Third Elements from Sublists
item8 = [['a', 10, 45], ['b', 3, 22], ['c', 5, 7], ['d', 1, 11]]
list(map(operator.itemgetter(0, 2), item8)) # [('a', 45), ('b', 22), ('c', 7), ('d', 11)]
# Using Lambda
list(map(lambda x : (x[0], x[2]), item8) # [('a', 45), ('b', 22), ('c', 7), ('d', 11)]
- Selecting Specific Elements from a List
item9 = [10, 21, 15, 44, 99, 47]
operator.itemgetter(1, 3, 4)(item9) # (21, 44, 99)
# Using Lambda
list(map(lambda x: item9[x], [1, 3, 4])) # (21, 44, 99)
- Getting Values of Two Keys in a Dictionary
item10 = {'Name':'Alice', 'Age':50, 'Weight':65}
operator.itemgetter('Age', 'Weight')(item10) # (50, 65)
# Using Lambda
list(map(lambda x: item10[x], ['Age', 'Weight'])) # [50, 65]
- Getting Values of Two Keys in a List of Dictionaries
item11 = [{'Name':'Alice', 'Age':50, 'Weight':65}, {'Name':'Bob', 'Age':30, 'Weight':65} , {'Name':'Charlie', 'Age':60, 'Weight':65}]
list(map(operator.itemgetter('Name', 'Weight'), item11)) # [('Alice', 65), ('Bob', 65), ('Charlie', 65)]
# Using Lambda
list(map(lambda x: (x['Name'], x['Weight']), item11)) # [('Alice', 65), ('Bob', 65), ('Charlie', 65)]
