<jupyter_start><jupyter_text>let's pause this course for a moment - don't you think its going a bit too fastwithout any good examples? that example was so confusing, don't know why they created itA lambda function is a small anonymous function.A lambda function can take any number of arguments, but can only have one expression.  Lambda variable_name = lambda_keyword argument_or_list_of_inputs : expression_or_return_result<jupyter_code>#first let's define a normal function
def multiply(a,b):
    return a*b

multiply(5,6)
#now let's use lambda
multiply_using_lambda = lambda a,b : a*b

multiply_using_lambda(5,6)
multiply_using_lambda
#define a lambda function that returns square of a value
square = lambda a : a*a

square(5)
#get 3 values and return a list

list_lambda = lambda a,b,c : [a,b,c]
    
list_lambda('apple',2,3)
#get a string and return the first character of a string

return_first_character = lambda input_string : input_string[0]

return_first_character('hello')
#return length

return_length = lambda input_string : len(input_string)

return_length('Good Morning')<jupyter_output><empty_output><jupyter_text>lambda using lists<jupyter_code>#create a function to get a list and square the items in it 

number_list = [1,2,3,4,5]

def square_list(input_list):
    squared_list = []
    for item in input_list:
        square = item * item
        squared_list.append(square)
    return squared_list

square_list(number_list)
        
#use map
#map syntax:
#map(function,list)

def square_numbers(item):
    return item * item

map(square_numbers,number_list)

#convert map object to a list
list(map(square_numbers,number_list))
#use lambda

square_lambda = lambda item : item * item

list(map(square_lambda,number_list))
#making it compact ie, get rid of the function and put lambda inside map

list(map(lambda item : item * item , number_list))
#convert a list of words to upper case
breakfast_menu = ['tea','coffee','biscuits']

list(map(lambda element : element.upper(), breakfast_menu))
#converst list of sentences to lower case
sentences = ['I had breakfast','I am having lunch','I will have dinner']

list(map(lambda sentence : sentence.lower(),sentences))<jupyter_output><empty_output><jupyter_text>Lambda with conditions<jupyter_code>#a function that returns true if value is even, false if odd
def odd_even(number):
    if (number%2==0):
        return True
    else:
        return False
    
odd_even(4)
odd_even(5)
#use lambda
odd_even_lambda = lambda number : True if number%2 == 0 else False

odd_even_lambda(6)
odd_even_lambda(7)
#normal function - filter out even numbers from a list
numbers = [12,23,34,45,56,67,78,89,90]

def filter_even(numbers_list):
    even_numbers = []
    for number in numbers_list:
        if(number%2==0):
            even_numbers.append(number)
    return even_numbers

filter_even(numbers)
#using filter function
#syntax - filter(function_name,list of objects) -> returns the items for which function 
#returned true
#filter can iterate through the list of objects like map
#see the example using lambda for this

#just recreate our old function
def odd_even(number):
    if (number%2==0):
        return True
    else:
        return False
filter(odd_even,numbers)
#convert the filter object to list ie, cast it
list(filter(odd_even,numbers))
#how about using our old lambda
#use lambda
odd_even_lambda = lambda number : True if number%2 == 0 else False
list(filter(odd_even_lambda,numbers))
list(filter(lambda number : True if number%2 == 0 else False,numbers))
#filter out the names starting with 'S'
names = ['Vivaan','Aarav','Krishna','Shazin','Indhu','Shiyon','Liyon','Niyon','Shilna','Kiran']

list(filter(lambda name : True if name[0]=='S' else False,names))
#names starting with K but with length 5


list(filter(lambda name : True if (name[0]=='K') and (len(name)==5) else False,names))
#mulitply odd by 10 and even by 100
list(map(lambda number: number*100 if number%2==0 else number*10,numbers))<jupyter_output><empty_output><jupyter_text>Lambda and sorting<jupyter_code>#a list of objects that contains products as tuples
products = [('Carrot',2),('Apple',10),('Egg',12),('Banana',5),('Dry Fruits',20)]

#sort it alphabetically
products.sort()
products
#using lambda - sort by price
products.sort(key = lambda item : item[1])

products
# a list of dictionaries
employees = [
                {"name":"Charles","Age": 34,"Income":30000},
                {"name":"Alex","Age":25 ,"Income":50000},
                {"name":"Emily","Age": 45,"Income":10000},
                {"name":"Ben","Age": 23,"Income":40000},
                {"name":"Dick","Age":33 ,"Income":20000},
            ]
employees
#sort by name
#employees.sort()-gives error

employees.sort(key = lambda person : person["name"])
employees
#based on income
employees.sort(key = lambda person : person["Income"])
employees<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11245144142302188, 'fraction_numerical': 0.022081373952157025, 'mean_word_length': 4.944106925880924, 'pattern_counts': {'":': 15, '<': 17, '<?xml version=': 0, '>': 18, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '50727', 'license': 'MIT', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1531, 'n_words': 489, 'path': 'Course1-Introduction to Data Science with Python/.ipynb_checkpoints/07_Lambda-checkpoint.ipynb', 'repo_name': 'leogithubid/Applied-Data-Science-with-Python', 'size': '4891'}