<jupyter_start><jupyter_text><jupyter_code># based on Efficient Python Programming with Lambda, Map, Filter, and Sorted at https://towardsdatascience.com/efficient-python-programming-with-lambda-map-filter-and-sorted-cfdf536afc36

# Lambda 
squared = lambda x: x*x
squared(5)
add = lambda x, y: x+y
add(4,5)
# Map 
# The map takes lists and a function and returns the list, modified by the function
a = [3,4,5,2,7,8]
b = [7,9,2,4,5,1]
c = [5,7,3,4,5,9]
add(a,b)
# typical element-wise add of two lists
res = []
for i in range(0, len(a)):
    res.append(a[i]+ b[i])
res
# Map works exactly like a ‘for loop’
list(map(add, a, b))
list(map(lambda x, y: x+y, a, b))
list(map(lambda x, y, z: x+y+z, a, b, c))
list(map(lambda x, y, z: 2*x + 2.5*y + z, a, b, c))
# Filter
# The filter takes a list as an argument and a function as the expression
# Filter to only greater than 5
c = [4,2,9,6,10,4,1]
list(filter(lambda x: x > 5, c))
# filter to those divisible by two
a = [3,4,5,2,7,8]
list(filter(lambda x: x % 2==0, a))
# Filter to start with A
names = ['Abram', 'Arib', 'Bob', 'Shawn', 'Aria', 'Cicilia', 'John']
list(filter(lambda x: x[0]=='A', names))
# Sorted
sorted(names, key=lambda x: x[0])
sorted(names)
sorted(names, reverse=True)
# key sorting 
# <NAME> Transforming Code into Beautiful, Idiomatic Python https://www.youtube.com/watch?feature=player_embedded&v=OSGv2VnC0go
# https://speakerdeck.com/pyconslides/transforming-code-into-beautiful-idiomatic-python-by-raymond-hettinger-1
# https://github.com/JeffPaine/beautiful_idiomatic_python and 
len_sorted = sorted(names, key=len)
print('len_sorted=',len_sorted)
# take the second element for sort
def take_second(elem):
    return elem[1]
second_letter_sorted = sorted(names, key=take_second)    
print('second_letter_sorted=',second_letter_sorted)
# Map on a dictionary
dict_a = [{'name': 'John', 'age': 12},{'name': 'Sonia', 'age': 10},{'name': 'Steven', 'age': 13},{'name': 'Natasha', 'age': 9}]
# just get the names
list(map(lambda x: x['name'], dict_a))
# just get the ages
list(map(lambda x: x['age'], dict_a))
# ages sorted
sorted(list(map(lambda x: x['age'], dict_a)))
# sort the whole dictionary by age
sorted(dict_a, key=lambda x: x['age'])
# get youngest
sorted(dict_a, key=lambda x: x['age'])[0]
# oldest's info
sorted(dict_a, key=lambda x: x['age'])[-1]
# decending age
sorted(dict_a, key=lambda x: x['age'], reverse =True)
# sort by alpha names
sorted(dict_a, key=lambda x: x['name'])
# children over 10
list(filter(lambda x: x['age'] > 10, dict_a))
# ages in 3 years
list(map(lambda x: x['age']+3, dict_a))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17181612660135645, 'fraction_numerical': 0.025998492840994723, 'mean_word_length': 5.061643835616438, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '482570', 'license': 'MIT', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1061, 'n_words': 289, 'path': 'efficient_programming/lambda_map_filter_and_sorted.ipynb', 'repo_name': 'nheske/learn-python', 'size': '2665'}