<jupyter_start><jupyter_text>Lambda Expressions, Map, and FilterNow its time to quickly learn about two built in functions, filter and map. Once we learn about how these operate, we can learn about the lambda expression, which will come in handy when you begin to develop your skills further!  map functionThe **map** function allows you to "map" a function to an iterable object. That is to say you can quickly call the same function to every item in an iterable, such as a list. For example:<jupyter_code>def square(num):
    return num**2
my_nums = [1,2,3,4,5]
map(square,my_nums)
for item in map(square,my_nums):
    print(item)
# To get the results, either iterate through map() 
# or just cast to a list
list(map(square,my_nums))<jupyter_output><empty_output><jupyter_text>The functions can also be more complex<jupyter_code>def splicer(mystring):
    if len(mystring) % 2 == 0:
        return 'EVEN'
    else:
        return mystring[0]
mynames = ['John','Cindy','Sarah','Kelly','Mike']
# When yo upass i nthe func map will run the function 
# so we dont need the () after the function
list(map(splicer,mynames))<jupyter_output><empty_output><jupyter_text>filter functionThe filter function returns an iterator yielding those items of iterable for which function(item)is true. Meaning you need to filter by a function that returns either True or False. Then passing that into filter (along with your iterable) and you will get back only the results that would return True when passed to the function.<jupyter_code>def check_even(num):
    return num % 2 == 0
nums = [0,1,2,3,4,5,6,7,8,9,10]
filter(check_even,nums)
# transform it
list(filter(check_even,nums))
# iterate through the results
for n in filter(check_even,nums):
    print(n)<jupyter_output>0
2
4
6
8
10<jupyter_text>lambda expressionOne of Pythons most useful (and for beginners, confusing) tools is the lambda expression. lambda expressions allow us to create "anonymous" functions. 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. There is key difference that makes lambda useful in specialized roles:**lambda's body is a single expression, not a block of statements.*** The lambda's body is similar to what we would put in a def body's return statement. We simply type the result as an expression instead of explicitly returning it. Because it is limited to an expression, a lambda is less general that a def. We can only squeeze design, to limit program nesting. lambda is designed for coding simple functions, and def handles the larger tasks. Lets slowly break down a lambda expression by deconstructing a function:<jupyter_code>def square(num):
    result = num**2
    return result
square(2)<jupyter_output><empty_output><jupyter_text>We could simplify it:<jupyter_code>def square(num):
    return num**2
square(2)<jupyter_output><empty_output><jupyter_text>We could actually even write this all on one line.<jupyter_code># Bad styling and not by convention
def square(num): return num**2
square(2)<jupyter_output><empty_output><jupyter_text>This is the form a function that a lambda expression intends to replicate. A lambda expression can then be written as:<jupyter_code>lambda num: num ** 2
# You wouldn't usually assign a name to a lambda expression, this is just for demonstration!
square = lambda num: num **2
square(2)<jupyter_output><empty_output><jupyter_text>So why would use this? Many function calls need a function passed in, such as map and filter. Often you only need to use the function you are passing in once, so instead of formally defining it, you just use the lambda expression. Let's repeat some of the examples from above with a lambda expression<jupyter_code>list(map(lambda num: num ** 2, my_nums))
list(filter(lambda n: n % 2 == 0,nums))<jupyter_output><empty_output><jupyter_text>Here are a few more examples, keep in mind the more comples a function is, the harder it is to translate into a lambda expression, meaning sometimes its just easier (and often the only way) to create the def keyword function. ** Lambda expression for grabbing the first character of a string: **<jupyter_code># check even with lambda
list(filter(lambda num: num%2 == 0, nums))
mynames
list(map(lambda mynames: mynames[0], mynames))
list(map(lambda x: x[::-1], mynames))
lambda s: s[0]<jupyter_output><empty_output><jupyter_text>** Lambda expression for reversing a string: **<jupyter_code>lambda s: s[::-1]<jupyter_output><empty_output><jupyter_text>You can even pass in multiple arguments into a lambda expression. Again, keep in mind that not every function can be translated into a lambda expression.<jupyter_code>lambda x,y : x + y<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07698675496688742, 'fraction_numerical': 0.009933774834437087, 'mean_word_length': 5.5665760869565215, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '235426', 'license': 'MIT', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1373, 'n_words': 630, 'path': '03-Methods and functions/05-Lambda-Expressions-Map-and-Filter.ipynb', 'repo_name': 'dglynn/python-bootcamp-udemy', 'size': '4832'}