Course Content
Intermediate Python Techniques
Intermediate Python Techniques
Built-in Functions
Here are two more examples demonstrating the use of lambda functions with the filter()
and sorted()
functions in Python.
Alternatively, you can pass a more complex custom function instead of a lambda to these high-order functions.
filter()
The filter()
function is used to create an iterator from elements of an iterable for which a function returns true. Here's an example using filter() with a lambda function to filter out odd numbers from a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Using a lambda function to filter out odd numbers even_numbers = filter(lambda x: x % 2 == 0, numbers) # Convert the filter object to a list even_numbers_list = list(even_numbers) print(even_numbers_list)
In this example, the lambda function lambda x: x % 2 == 0
checks if a number is even. The filter()
function applies this lambda to each element in the list of numbers and returns an iterator of even numbers.
sorted()
The sorted()
function returns a new sorted list from the elements of any iterable.
iterable
is the sequence to sort (list, dict, tuple, etc);key
is a function to execute to decide the order;reverse
is a boolean. False is an ascending order, and True is descending. The default is False.
Here's an example using sorted()
with a lambda function to sort a list of tuples based on the second element in each tuple:
tuples = [(1, 'banana'), (2, 'apple'), (3, 'orange')] # Using a lambda function to sort by the second element of each tuple sorted_tuples = sorted(tuples, key=lambda x: x[1]) print(sorted_tuples)
In this example, the lambda function lambda x: x[1]
returns the second element of each tuple. The sorted()
function then sorts the list tuples based on these second elements, resulting in a list sorted alphabetically by the fruit names.
Swipe to show code editor
Let's consider a list of dictionaries representing books, and you want to filter out books that have a certain number of pages.
- Define the list of books.
books
is a list of dictionaries. Each dictionary represents a book with two keys: "title" and "pages". - Creating the custom function
has_many_pages
that acceptsbook
andmin_pages
arguments. - Use
filter()
with the custom function. - Converte the filter object to a list, store it to the
filtered_books_list
variable, and print it.
Thanks for your feedback!
Built-in Functions
Here are two more examples demonstrating the use of lambda functions with the filter()
and sorted()
functions in Python.
Alternatively, you can pass a more complex custom function instead of a lambda to these high-order functions.
filter()
The filter()
function is used to create an iterator from elements of an iterable for which a function returns true. Here's an example using filter() with a lambda function to filter out odd numbers from a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Using a lambda function to filter out odd numbers even_numbers = filter(lambda x: x % 2 == 0, numbers) # Convert the filter object to a list even_numbers_list = list(even_numbers) print(even_numbers_list)
In this example, the lambda function lambda x: x % 2 == 0
checks if a number is even. The filter()
function applies this lambda to each element in the list of numbers and returns an iterator of even numbers.
sorted()
The sorted()
function returns a new sorted list from the elements of any iterable.
iterable
is the sequence to sort (list, dict, tuple, etc);key
is a function to execute to decide the order;reverse
is a boolean. False is an ascending order, and True is descending. The default is False.
Here's an example using sorted()
with a lambda function to sort a list of tuples based on the second element in each tuple:
tuples = [(1, 'banana'), (2, 'apple'), (3, 'orange')] # Using a lambda function to sort by the second element of each tuple sorted_tuples = sorted(tuples, key=lambda x: x[1]) print(sorted_tuples)
In this example, the lambda function lambda x: x[1]
returns the second element of each tuple. The sorted()
function then sorts the list tuples based on these second elements, resulting in a list sorted alphabetically by the fruit names.
Swipe to show code editor
Let's consider a list of dictionaries representing books, and you want to filter out books that have a certain number of pages.
- Define the list of books.
books
is a list of dictionaries. Each dictionary represents a book with two keys: "title" and "pages". - Creating the custom function
has_many_pages
that acceptsbook
andmin_pages
arguments. - Use
filter()
with the custom function. - Converte the filter object to a list, store it to the
filtered_books_list
variable, and print it.
Thanks for your feedback!
Built-in Functions
Here are two more examples demonstrating the use of lambda functions with the filter()
and sorted()
functions in Python.
Alternatively, you can pass a more complex custom function instead of a lambda to these high-order functions.
filter()
The filter()
function is used to create an iterator from elements of an iterable for which a function returns true. Here's an example using filter() with a lambda function to filter out odd numbers from a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Using a lambda function to filter out odd numbers even_numbers = filter(lambda x: x % 2 == 0, numbers) # Convert the filter object to a list even_numbers_list = list(even_numbers) print(even_numbers_list)
In this example, the lambda function lambda x: x % 2 == 0
checks if a number is even. The filter()
function applies this lambda to each element in the list of numbers and returns an iterator of even numbers.
sorted()
The sorted()
function returns a new sorted list from the elements of any iterable.
iterable
is the sequence to sort (list, dict, tuple, etc);key
is a function to execute to decide the order;reverse
is a boolean. False is an ascending order, and True is descending. The default is False.
Here's an example using sorted()
with a lambda function to sort a list of tuples based on the second element in each tuple:
tuples = [(1, 'banana'), (2, 'apple'), (3, 'orange')] # Using a lambda function to sort by the second element of each tuple sorted_tuples = sorted(tuples, key=lambda x: x[1]) print(sorted_tuples)
In this example, the lambda function lambda x: x[1]
returns the second element of each tuple. The sorted()
function then sorts the list tuples based on these second elements, resulting in a list sorted alphabetically by the fruit names.
Swipe to show code editor
Let's consider a list of dictionaries representing books, and you want to filter out books that have a certain number of pages.
- Define the list of books.
books
is a list of dictionaries. Each dictionary represents a book with two keys: "title" and "pages". - Creating the custom function
has_many_pages
that acceptsbook
andmin_pages
arguments. - Use
filter()
with the custom function. - Converte the filter object to a list, store it to the
filtered_books_list
variable, and print it.
Thanks for your feedback!
Here are two more examples demonstrating the use of lambda functions with the filter()
and sorted()
functions in Python.
Alternatively, you can pass a more complex custom function instead of a lambda to these high-order functions.
filter()
The filter()
function is used to create an iterator from elements of an iterable for which a function returns true. Here's an example using filter() with a lambda function to filter out odd numbers from a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Using a lambda function to filter out odd numbers even_numbers = filter(lambda x: x % 2 == 0, numbers) # Convert the filter object to a list even_numbers_list = list(even_numbers) print(even_numbers_list)
In this example, the lambda function lambda x: x % 2 == 0
checks if a number is even. The filter()
function applies this lambda to each element in the list of numbers and returns an iterator of even numbers.
sorted()
The sorted()
function returns a new sorted list from the elements of any iterable.
iterable
is the sequence to sort (list, dict, tuple, etc);key
is a function to execute to decide the order;reverse
is a boolean. False is an ascending order, and True is descending. The default is False.
Here's an example using sorted()
with a lambda function to sort a list of tuples based on the second element in each tuple:
tuples = [(1, 'banana'), (2, 'apple'), (3, 'orange')] # Using a lambda function to sort by the second element of each tuple sorted_tuples = sorted(tuples, key=lambda x: x[1]) print(sorted_tuples)
In this example, the lambda function lambda x: x[1]
returns the second element of each tuple. The sorted()
function then sorts the list tuples based on these second elements, resulting in a list sorted alphabetically by the fruit names.
Swipe to show code editor
Let's consider a list of dictionaries representing books, and you want to filter out books that have a certain number of pages.
- Define the list of books.
books
is a list of dictionaries. Each dictionary represents a book with two keys: "title" and "pages". - Creating the custom function
has_many_pages
that acceptsbook
andmin_pages
arguments. - Use
filter()
with the custom function. - Converte the filter object to a list, store it to the
filtered_books_list
variable, and print it.