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#

  1. Write a function to double a list

  2. Use list comprehension to double a list

  3. 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

  1. using a list and loop

  2. using list comprehension

  3. 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

  1. using a loop and dict

  2. using filter without a function

years = range(1970, 2000) # for these years

Exercise 8#

Sort x using value

x = (('efg', 1), ('abc', 3), ('hij', 2))

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 11#

Sort array by len of names

students = ['john', 'Janette', 'doe']

Exercise 12#

Sort dictionary by value

my_list = [{'value': '34.4'}, {'value': '45.3'}, {'value': '73.4'}]