The document discusses various built-in functions in Python including numeric, string, and container data types. It provides examples of using list comprehensions, dictionary comprehensions, lambda functions, enumerate, zip, filter, any, all, map and reduce to manipulate data in Python. It also includes references to online resources for further reading.
9. zip
• my_first_list = "abcde"
my_second_list = "zyxwv"
result = zip(my_first_list, my_second_list)
print(result)
result2 = [''.join(x) for x in result]
print(result2)
result3 = ['123'.join(x) for x in result]
print(result3)
print(dict(result))
print([(k*3,v) for k,v in result])
[('a', 'z'), ('b', 'y'), ('c', 'x'), ('d', 'w'), ('e', 'v')]
['az', 'by', 'cx', 'dw', 'ev']
['a123z', 'b123y', 'c123x', 'd123w', 'e123v']
{'a': 'z', 'c': 'x', 'b': 'y', 'e': 'v', 'd': 'w'}
[('aaa', 'z'), ('bbb', 'y'), ('ccc', 'x'), ('ddd', 'w'), ('eee', 'v')]
10. filter
• my_list = [1,2,3,4,5,6]
print([x for x in my_list if x % 2 == 0])
print(filter(lambda x: x % 2 == 0, my_list))
#filter(function, iterable)
[2, 4, 6]
[2, 4, 6]
11. Any / all
• my_list = [True,False,False,False]
print(any(my_list))
print(all(my_list))
my_list2 = [True,True,True]
print(any(my_list2))
print(all(my_list2))
True
False
True
True
12. • all(iterable)Return True if all elements of the iterable are true (or if the
iterable is empty). Equivalent to:
• def all(iterable):
for element in iterable:
if not element:
return False
return True
• any(iterable)Return True if any element of the iterable is true. If the iterable
is empty, return False. Equivalent to:
• def any(iterable):
for element in iterable:
if element: return True
return False
14. reduce
• val = 0
for x in range(1,7):
val += x
print(val)
print(reduce(lambda x,y: x+y, range(1,7)))
print(reduce(lambda x,y: x*y, range(1,7)))
print(sum(range(1,7)))
21
21
720
21