Lambda, Filter, Map, and Sorted
Contents
Lambda, Filter, Map, and Sorted#
Exercise 1#
Write a function that squares a number and returns the value. Write it again again using Lambda functions
Exercise 2#
Write a lambda function for adding two numbers
Exercise 3#
Write a lambda function that multiples two numbers. Use the lambda function as an input to another function.
Exercise 4#
Write a function to double a list
Use list comprehension to double a list
Use lambda with map to double a list
map objects are a generator. How many times can you iterate over them?
my_list = [2, 3, 4]
Exercise 5#
Capitalize the names in the students list
using a list and loop
using list comprehension
using lambda and map
students = ['john', 'jane', 'doe']
Exercise 6#
Using lambda and map, convert the numbers to float
covert each value to float#
my_dict = [{'value': '34.4'}, {'value': '45.3'}, {'value': '73.4'}]
Exercise 7#
Create a dictionary where the key is year and value is True/False if the year is a leap year
using a loop and dict
using filter without a function
years = range(1970, 2000) # for these years
Exercise 9#
sort dictionary by username; reverse sort
students = [
{'username': 'john', 'grade': 50},
{'username': 'jane', 'grade': 80},
{'username': 'doe', 'grade': 35},
{'grade': 89, 'username': 'Kelly'}
]
Exercise 10#
Sort dictionary by grade; reverse sort
students = [
{'username': 'john', 'grade': 50},
{'username': 'jane', 'grade': 80},
{'username': 'doe', 'grade': 35},
{'grade': 89, 'username': 'Kelly'}
]
Exercise 12#
Sort dictionary by value
my_list = [{'value': '34.4'}, {'value': '45.3'}, {'value': '73.4'}]