5 Best Ways to Find Final Prices With a Special Discount in a Shop Using Python

Rate this post

πŸ’‘ Problem Formulation: Imagine you’re managing a point-of-sale system, and you need to calculate the final price for each item after applying a special discount. The input includes a list of item prices and a flat discount percentage applicable to all. The desired output is a list containing the final prices of the items after applying the discount.

Method 1: Using a For Loop

This method implements a straightforward approach to apply discounts to prices using a for loop. It iterates through each item, calculates the discount, and appends the final price to a new list. This is good for those who appreciate explicit code and readability.

Here’s an example:

prices = [100, 200, 300]
discount_percentage = 10

def calculate_discounted_prices(prices, discount_percentage):
    final_prices = []
    for price in prices:
        discount = price * discount_percentage / 100
        final_prices.append(price - discount)
    return final_prices

print(calculate_discounted_prices(prices, discount_percentage))

Output: [90.0, 180.0, 270.0]

This code snippet defines a function calculate_discounted_prices that takes a list of item prices and a discount percentage. It loops over each price, calculates the discount by multiplying the price and discount percentage, then subtracts it from the original price to get the final price. The final prices are returned as a list after the loop completes.

Method 2: Using List Comprehension

List comprehensions in Python offer a more concise and readable way to create lists, including the application of operations like discounts to prices. This method is faster and considered more Pythonic compared to the counterpart with loops.

Here’s an example:

prices = [100, 200, 300]
discount_percentage = 10

def calculate_discounted_prices(prices, discount_percentage):
    return [price - (price * discount_percentage / 100) for price in prices]

print(calculate_discounted_prices(prices, discount_percentage))

Output: [90.0, 180.0, 270.0]

This code snippet uses list comprehension to calculate and return the discounted prices in one line of code. The expression inside the brackets calculates the final price for each item by subtracting the discount from the original price, which is all done in one concise and efficient operation.

Method 3: Using the map Function

The map function is a built-in Python function that applies the same operation to each item of an iterable. Using this method, one can apply the discount to all items using less code, improving readability and efficiency for large datasets.

Here’s an example:

prices = [100, 200, 300]
discount_percentage = 10

def apply_discount(price, discount_percentage):
    return price - (price * discount_percentage / 100)

final_prices = list(map(apply_discount, prices, [discount_percentage] * len(prices)))

print(final_prices)

Output: [90.0, 180.0, 270.0]

This snippet demonstrates the use of map by declaring a function called apply_discount, which calculates the discounted price. map applies this function across the entire list of prices, taking the discount rate as an argument. A list of the same length as ‘prices’ is created to supply the discount value to each price.

Method 4: Using Lambda Functions

By using anonymous functions in Python, known as lambda functions, this method allows you to define the discount operation inline without the need for a separate function definition, making it concise for one-off uses.

Here’s an example:

prices = [100, 200, 300]
discount_percentage = 10

final_prices = list(map(lambda price: price - (price * discount_percentage / 100), prices))

print(final_prices)

Output: [90.0, 180.0, 270.0]

In the above code, the lambda function replaces the need for a defined function like in Method 3. The lambda function is defined directly in the call to map, resulting in a succinct one-liner that executes the discount calculation for each element in the ‘prices’ list.

Bonus One-Liner Method 5: Using NumPy

If you’re working in a data science context or need to perform this operation on a large scale, NumPy’s vectorized operations provide a highly efficient method. This one-liner is not only rapid but also incredibly succinct, capitalizing on NumPy’s powerful array operations.

Here’s an example:

import numpy as np

prices = np.array([100, 200, 300])
discount_percentage = 10

final_prices = prices - (prices * discount_percentage / 100)

print(final_prices)

Output: [ 90. 180. 270.]

This snippet makes use of NumPy’s vectorized operations by first converting the price list to a NumPy array. The discount operation is then performed on the entire array in one go, which is much faster than iterating over each element, especially with large amounts of data.

Summary/Discussion

  • Method 1: For Loop. Easy to understand and perfect for beginners. However, it’s not the most efficient for large datasets.
  • Method 2: List Comprehension. Much clearer and often faster than a for loop. Still not the most performant option but definitely a more Pythonic one.
  • Method 3: Map Function. Can lead to slightly more readable code and is faster than a for loop but less intuitive to those unfamiliar with functional programming concepts.
  • Method 4: Lambda Functions. Offers a quick, one-off solution without polluting the namespace with additional function definitions. However, it can be less readable for complex operations.
  • Bonus Method 5: Using NumPy. High-performance option for large datasets with instantaneous computation. Requires an external library and understanding of NumPy arrays.