<jupyter_start><jupyter_text>Lambda ExpressionsLambda expressions allow us to create "anonymous" functions i.e functions without a name. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.Function objects returned by running lambda expressions work exactly the same as those created and assigned by defs.However the following are the key differences between lambda functions and 'def' functions.1. Lambda's body is a single expression, not a block of statements2. 'Lambda' is designed for coding simple functions and 'def' handles the larger tasks.<jupyter_code># Normal function

def square(num):
    result = num**2
    return result

square(2)
# Simplified Version #1
def square(num):
    return num**2

square(3)
# Simplified Version #1
def square(num):return num**2

square(4)<jupyter_output><empty_output><jupyter_text>The syntax for lambda function is quite simple.lambda argument_list:expressionThe argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments.The following example returns the square of a given number.<jupyter_code>square = lambda num: num **2
square(5)
sum1 = lambda x,y: x+y
print(sum1(3,4))
even = lambda x: x%2==0
print(even(12))
first = lambda str: str[0]
print(first('Hello'))
rev = lambda str:str[::-1]
print(rev('Hello'))<jupyter_output>7
True
H
olleH<jupyter_text>Filter FunctionThe function filter(function,list) offers an elegant way to filter out all the elements of a list, for which the function **__function__** returns True.The function filter(f,1) needs a function f as its first argument. f returns a Boolean value i.e. either True or False. This function will be applied to every element of the list l. Only if f returns True will the element of the list included in the resultset.<jupyter_code>nums = [2,3,4,7,9,10]
evens = list(filter(lambda x: x%2==0,nums))
print(evens)<jupyter_output>[2, 4, 10]<jupyter_text>Map Functionmap is a function with 2 arguments. result = map(function,seq)The first argument func is the name of a function and the second a sequence (e.g. a list) seq. map() applies the function func to all the elements of the sequence seq. It returns a new list with all the elements changed by func.<jupyter_code>def far(T):
    return ((float(9)/5)* T + 32)
def cel(T):
    return (float(5)/9) * (T - 32)

temp = (0,35,90,125)
F = map(far,temp)
temp1 = list(F)
C = map(cel,temp1)
print(temp1)
print(list(C))
temp = (0,35,90,125)
f = map(lambda x: (float(9)/5)*x +32,temp)
f_list = list(f)
c = map(lambda x: (float(5)/9) * (x -32),f_list)
print(f_list)
print(list(c))<jupyter_output>[32.0, 95.0, 194.0, 257.0]
[0.0, 35.0, 90.0, 125.0]<jupyter_text>map() can also be applied to more than one list but the lists must have the same length. map() will apply its lambda function to the elements of the argument lists, i.e. it first applies to the elements with the 0th index, then to the elements with the 1st index until the n-th index is reached:<jupyter_code>a = [1,2,3,4]
b = [5,6,7,8]
c = [-1,-2,1,2]

list(map(lambda x,y,z:x+y-z,a,b,c))<jupyter_output><empty_output><jupyter_text>Reduce Functionreduce(func,seq)If seq = [s1,s2,s3,...,sn], calling reduce(func,seq) works like this:* At first the two elements of seq will be applied to func, i,e func(s1,s2). The list on which reduce() works looks now like this: [func(s1,s2),s3,...,sn]* In the next step func will be applied on the previous result and the third element on the list, i.e.func(func(s1,s2),s3). The list looks like this now: [func(func(s1,s2),s3),...,sn]* Continue like this until just one element is left and return this element as the result of reduce()<jupyter_code>from functools import reduce
reduce(lambda x,y: x+y, [47,23,11,34])
from functools import reduce
reduce(lambda x,y: x if (x > y) else y, [47,12,33,95])
from functools import reduce
reduce(lambda x,y: x * y, range(1,10,2))<jupyter_output><empty_output><jupyter_text>Zip Functionzip() makes an iterator that aggregates elements from each of the iterables.<jupyter_code>a = [1,2,3]
b = [4,5,6]

list(zip(a,b))<jupyter_output><empty_output><jupyter_text>zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. Only the shortest iterable itme will be taken and any extra elements will be ignored<jupyter_code># Example - 1
x = [1,2,3]
y = [4,5]

list(zip(x,y))
# Example - 2
a = ['a','b']
b = ['c','d','e']

list(zip(a,b))<jupyter_output><empty_output><jupyter_text>Zip with Dictionary<jupyter_code>d1 = {'a':1,'b':2}
d2 = {'c':3,'c':4}
print(list(zip(d1,d2)))<jupyter_output>[('a', 'c')]<jupyter_text>This makes sense because simply iterating through the dictionaries will result in just the keys. We would have to call methods to mix keys and values<jupyter_code>def swapdic(d1,d2):
    dout = {}
    
    for d1key,d2val in zip(d1,d2.values()):
        dout[d1key] = d2val
    
    return dout

swapdic(d1,d2)<jupyter_output><empty_output><jupyter_text>Enumerate FunctionReturn an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. Returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.<jupyter_code>colors = ['Blue','Black','White','Red']
list(enumerate(colors))
list(enumerate(colors,1))
for index,item in enumerate(colors):
    print(index)
    print(item)

for i,j in enumerate(colors):
    if i == 2:
        break
    print(j)<jupyter_output>Blue
Black<jupyter_text>Any / All<jupyter_code>lst = [True, True, False, True]
all(lst)
any(lst)<jupyter_output><empty_output><jupyter_text>Complexcomplex() returns a complex number with the value real + imag*1j or converts a string or number to a complex number.<jupyter_code>complex()  # if no arguments are given results in 0j
complex(2,4)
complex('2') # if imag part is omitted substitutes with 0j
complex('2','3') # this will create an error as the second parameter cannot be a string.
complex('1+2j')
complex('3',2)  # cannot take a secord argument if first is a strink<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11589511168662998, 'fraction_numerical': 0.03123988345742959, 'mean_word_length': 5.416407061266875, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '222791', 'license': 'MIT', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2050, 'n_words': 754, 'path': 'Lambda Functions.ipynb', 'repo_name': 'vravishankar/Jupyter-Books', 'size': '6178'}