Skip to content

Instantly share code, notes, and snippets.

@guestPK1986
Last active August 30, 2020 19:44
Show Gist options
  • Save guestPK1986/d2125aeca5714dfde3925681d9e0219e to your computer and use it in GitHub Desktop.
Save guestPK1986/d2125aeca5714dfde3925681d9e0219e to your computer and use it in GitHub Desktop.
map(), filter(), zip(), reduce(), lambda
#1 Capitalize first letter of all of the pet names and print the list
my_pets = ['sisi', 'bibi', 'titi', 'carla']
def first_upper(item):
for x in item:
up_char = item[0].upper()
item = up_char + item[1:]
return item
print(list(map(first_upper, my_pets)))
#OR
print(list(map(lambda item:item[0].upper()+item[1:], my_pets)))
#1a Capitalize all of the pet names and print the list
my_pets = ['sisi', 'bibi', 'titi', 'carla']
def capitalize(string):
return string.upper()
print(list(map(capitalize, my_pets)))
#OR
print(list(map(lambda item:item.upper(),my_pets)))
#2 Zip the 2 lists into a list of tuples, but sort the numbers from lowest to highest.
my_strings = ['a', 'b', 'c', 'd', 'e']
my_numbers = [5,4,3,2,1]
my_numbers.sort()
print(list(zip(my_strings, my_numbers)))
#3 Filter the scores that pass over 50%
scores = [73, 20, 65, 19, 76, 100, 88]
def above_50(item):
return item > 50
print(list(filter(above_50, scores)))
#OR
print(list(filter(lambda item:item > 50, scores)))
#4 Combine all of the numbers that are in a list my_numbers and scores (separately) using reduce().
from functools import reduce
my_numbers = [5,4,3,2,1]
scores = [73, 20, 65, 19, 76, 100, 88]
def accumulator(acc, item):
return acc + item
print (reduce(accumulator, scores, 0))
print (reduce(accumulator, my_numbers, 0))
#OR
print(reduce(lambda acc, item: acc + item, scores, 0))
print(reduce(lambda acc, item: acc + item, my_numbers, 0))
#4a Combine all of the numbers from lists my_numbers and scores (together) using reduce().What is the total?
print(reduce(accumulator, (my_numbers + scores)))
#OR
print(reduce(lambda acc, item: acc + item, my_numbers + scores, 0))
#5 From my_list print a list, where each number will be multiplied by 2
my_list = [1,2,3]
your_list = [10,20,30]
def multiply_by2(item):
return item*2 #creates new map object which multiplies iterable item by 2
print(list(map(multiply_by2, my_list))) # pure map function didnt affect outside world = my_list, it was outside, so my_list still the same [1,2,3]
# OR
print(list(map(lambda item:item*2, my_list)))
#6 Filter my_list to see only odd numbers
def only_odd(item):
return item%2 !=0
print(list(filter(only_odd, my_list)))
# OR
print(list(filter(lambda item:item%2 !=0, my_list)))
#7 Zip the 2 lists into a list of tuples
print(list(zip(my_list, your_list)))
#OR
print(list(map(lambda x, y: (x,y), my_list, your_list)))
#8 Combine all of the numbers that are in my_list using reduce
from functools import reduce
def accumulator(acc, item):
print(acc, item)
return acc + item
print (reduce(accumulator, my_list, 0))
#9 Given list my_list. Using lambda expression create a new tuples list,
# where all the first tuple values are same as in list my_list, and the second tuple values the sorted second tuple values from the list my_list.
my_list = [(0,2), (9,9), (4,3), (10,-1)]
new_first = []
new_second= []
for x in a:
new_first.append(x[0])
new_second.append(x[1])
print(list(map(lambda x,y:(x,y),new_first, sorted(new_second))))
#9 Given list my_list. Using lambda expression sort the list my_list based on the second tuples values.
my_list = [(0,2), (9,9), (4,3), (10,-1)]
my_list.sort(key = lambda x:x[1])
print(my_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment