Program to perform given operation with each element of a list and given value in Python

Suppose we have a list of numbers called nums, an operator string op representing operations like "+", "-", "/", or "*", and a value val. We need to perform the operation on every number in nums with val and return the result.

For example, if the input is [5, 3, 8] with operator "*" and value 3, the output will be [15, 9, 24].

Algorithm Steps

To solve this problem, we will follow these steps ?

  • Create an empty result list
  • For each number in the input list:
    • If operator is "+", add the value to the number
    • If operator is "-", subtract the value from the number
    • If operator is "*", multiply the number by the value
    • If operator is "/" and value is non-zero, divide the number by the value
  • Append each result to the result list
  • Return the result list

Implementation Using Class Method

Here's the implementation using a class-based approach ?

class Solution:
    def solve(self, nums, op, val):
        res = []
        for i in nums:
            if op == '+':
                res.append(i + val)
            elif op == '-':
                res.append(i - val)
            elif op == '*':
                res.append(i * val)
            elif op == '/' and val != 0:
                res.append(i // val)  # Integer division
        return res

# Test the solution
ob = Solution()
nums = [5, 3, 8]
result = ob.solve(nums, '*', 3)
print("Input:", nums)
print("Operation: * 3")
print("Output:", result)

The output of the above code is ?

Input: [5, 3, 8]
Operation: * 3
Output: [15, 9, 24]

Using Function Approach

We can also implement this using a simple function ?

def perform_operation(nums, op, val):
    result = []
    for num in nums:
        if op == '+':
            result.append(num + val)
        elif op == '-':
            result.append(num - val)
        elif op == '*':
            result.append(num * val)
        elif op == '/' and val != 0:
            result.append(num / val)  # Float division
    return result

# Test with different operations
numbers = [10, 20, 30]

print("Addition:", perform_operation(numbers, '+', 5))
print("Subtraction:", perform_operation(numbers, '-', 5))
print("Multiplication:", perform_operation(numbers, '*', 2))
print("Division:", perform_operation(numbers, '/', 5))

The output of the above code is ?

Addition: [15, 25, 35]
Subtraction: [5, 15, 25]
Multiplication: [20, 40, 60]
Division: [2.0, 4.0, 6.0]

Using List Comprehension

A more concise approach using dictionary mapping and list comprehension ?

def apply_operation(nums, op, val):
    operations = {
        '+': lambda x: x + val,
        '-': lambda x: x - val,
        '*': lambda x: x * val,
        '/': lambda x: x / val if val != 0 else 0
    }
    
    return [operations[op](num) for num in nums]

# Test the solution
data = [12, 8, 16, 4]
print("Original:", data)
print("Divide by 4:", apply_operation(data, '/', 4))
print("Add 10:", apply_operation(data, '+', 10))

The output of the above code is ?

Original: [12, 8, 16, 4]
Divide by 4: [3.0, 2.0, 4.0, 1.0]
Add 10: [22, 18, 26, 14]

Conclusion

This problem demonstrates basic arithmetic operations on lists in Python. The class-based approach provides structure, while list comprehension offers a more concise solution. Always handle division by zero to avoid runtime errors.

Updated on: 2026-03-25T10:50:36+05:30

422 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements