Think Python: How to Think Like a Computer Scientist

(singke) #1

contains only the uppercase strings:


def only_upper(t):
res = []
for s in t:
if s.isupper():
res.append(s)
return res

isupper is a string method that returns True if the string contains only uppercase letters.


An operation like only_upper is called a filter because it selects some of the elements and


filters out the others.


Most common list operations can be expressed as a combination of map, filter and reduce.

Free download pdf