<jupyter_start><jupyter_text>Copyright 2019 Google LLC.<jupyter_code># Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.<jupyter_output><empty_output><jupyter_text>Intermediate Python - Lambdas In this Colab, we will move on to the concept of lambda functions. You'll learn what a lambda function is and how to use them in your code.  Lambdas You have previously seen how to create named functions and call them.<jupyter_code>def my_function():
  print("Hello ML")

my_function()<jupyter_output><empty_output><jupyter_text>What you might not know is that functions are first-class objects in Python, so you can treat them as objects. For example, you can assign functions to variables:<jupyter_code>def say_hello():
  print("Hello")

greeting = say_hello

greeting()<jupyter_output><empty_output><jupyter_text>And you can pass functions to other functions:<jupyter_code>def say_hello():
  print("Hello")

def call_another_function(f):
  print("Calling function {}".format(f))
  f()

call_another_function(say_hello)<jupyter_output><empty_output><jupyter_text>Often, functions can be defined in one line, so it would be nice to have a shorthand rather than having to use the `def` notation. This is exactly the use case for a **`lambda` function**.Lambda functions start with the `lambda` keyword. A colon signals the start of the function body.<jupyter_code>my_lambda_func = lambda: print("This is a lambda function")

my_lambda_func()<jupyter_output><empty_output><jupyter_text>Lambda functions can accept arguments. Just put the variable names between the `lambda` keyword and the colon:<jupyter_code>add_one = lambda x: x + 1
print(add_one(2))

add = lambda x, y: x + y
print(add(1, 2))<jupyter_output><empty_output><jupyter_text>Sometimes lambda functions do not even need to be named. For example, when you are passing a function to another object (e.g. another function), you can pass a lambda function directly.<jupyter_code>def call_another_function(f):
  print("Calling function {}".format(f))
  f()

call_another_function(lambda: print("This is a lambda function"))<jupyter_output><empty_output><jupyter_text>There are many places where it is Pythonic to use lambdas instead of named functions. Some examples are the [map](https://docs.python.org/3/library/functions.htmlmap), [filter](https://docs.python.org/3/library/functions.htmlfilter), and [sorted](https://docs.python.org/3/library/functions.htmlsorted) built-in functions.One of the most standard use cases of a lambda function is to apply a function to every element of a list. You can do this using `map`. The code below adds one to every element in the list. (We end up needing the [list](https://docs.python.org/3/library/functions.htmlfunc-list) function to make the return value of map become a list again.)<jupyter_code>my_list = [4, 7, 9, 12, 34, 67]

def add_one(x):
  return x + 1

list(map(add_one, my_list))<jupyter_output><empty_output><jupyter_text>With a lambda function, this can be done in one line.<jupyter_code>my_list = [4, 7, 9, 12, 34, 67]

list(map(lambda x: x + 1, my_list))<jupyter_output><empty_output><jupyter_text>Exercises  Exercise 1 Use the [map](https://docs.python.org/3/library/functions.htmlmap) function and a lambda to change all of the following strings to uppercase.  Student Solution<jupyter_code>my_strings = ["I", "love", "LaMbDa", "Functions"]

### YOUR CODE HERE ###<jupyter_output><empty_output><jupyter_text>---  Exercise 2 Use the [sorted](https://docs.python.org/3/library/functions.htmlsorted) function and a lambda to sort the values in `my_list` by the "sort_order" value in each dictionary contained within the list.  Student Solution<jupyter_code>my_list = [
  {"value": 123, "sort_order": 1},
  {"value": 543, "sort_order": 0},
  {"value": 101, "sort_order": 4},
  {"value": 654, "sort_order": 3},
]

### YOUR CODE HERE ###
my_list = [
  {"value": 123, "sort_order": 1},
  {"value": 543, "sort_order": 0},
  {"value": 101, "sort_order": 4},
  {"value": 654, "sort_order": 3},
]

sorted(my_list, key=lambda x: x["sort_order"])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11145171885263849, 'fraction_numerical': 0.015765272607838846, 'mean_word_length': 5.6491994177583695, 'pattern_counts': {'":': 16, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 7, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '288640', 'license': 'Apache-2.0', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1418, 'n_words': 513, 'path': 'content/00_prerequisites/01_intermediate_python/02-lambdas.ipynb', 'repo_name': 'skykykykykykykykykykykys/applied-machine-learning-intensive', 'size': '4567'}