Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Arbitrary Keyword Arguments | Arbitrary Arguments
Python Functions Tutorial

book
Arbitrary Keyword Arguments

In programming, there is a special syntax for passing any number of named parameters to a function, known as **kwargs.

**kwargs allows a function to accept any number of named arguments and treat them as a dictionary.

def example_function(**kwargs):
for key, value in kwargs.items():
print(f'{key}: {value}')

# Example function call
example_function(name='John', age=25, city='New York')
123456
def example_function(**kwargs): for key, value in kwargs.items(): print(f'{key}: {value}') # Example function call example_function(name='John', age=25, city='New York')
copy

In this example, **kwargs receives named arguments and prints their keys and values.

Note

The .items() method is used to obtain a list of key-value pairs from a dictionary in Python. Each element in this list is represented as a tuple (key, value).

Task

Swipe to start coding

Implement a function that filters products based on a given budget. The function should return a list of affordable products or indicate if no products are available within the budget.

  1. You’re given the function filter_products_by_budget with input parameters budget and arbitrary keyword arguments (kwargs).
  2. In the for loop, use the items() method on kwargs to retrieve the product and price variables.
  3. Check if the budget variable is greater than or equal to the price variable.
  4. If the condition is met, add the product to the affordable_products dictionary, where the key is the product variable and the value is the price variable.
  5. If no products are within the budget, return the message "No products available within the budget."
  6. If at least one affordable product is found, Return the message "Available products within budget: {affordable_products}", where {affordable_products} is the dictionary of available products.

Solution

def filter_products_by_budget(budget, **kwargs):
affordable_products = {}
for product, price in kwargs.items():
if price <= budget:
affordable_products[product] = price
if not affordable_products:
return "No products available within the budget."
return f"Available products within budget: {affordable_products}"

# Testing the result
print(filter_products_by_budget(100, laptop=1200, smartphone=800, mouse=50, keyboard=90, headphones=150))
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
def filter_products_by_budget(budget, **kwargs):
affordable_products = {}
for product, price in ___
if ___:
affordable_products[___] = ___
if ___:
return "No products available within the budget."
return f"Available products within budget: {affordable_products}"

# Testing the result
print(filter_products_by_budget(100, laptop=1200, smartphone=800, mouse=50, keyboard=90, headphones=150))
toggle bottom row
some-alt