Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

filter in python

"""filter() is a higher-order built-in function that takes a function and iterable as 
inputs and returns an iterator with the elements from the iterable for which the function 
returns True"""
"""The code below uses filter() to get the names in cities that are fewer than 10 
characters long to create the list short_cities"""

cities = ["New York City", "Los Angeles", "Chicago", "Mountain View", "Denver", "Boston"]

def is_short(name):
    return len(name) < 10

short_cities = list(filter(is_short, cities))
print(short_cities)

>>>['Chicago', 'Denver', 'Boston']
Source by classroom.udacity.com #
 
PREVIOUS NEXT
Tagged: #filter #python
ADD COMMENT
Topic
Name
4+6 =