Python - One Liner!
We define a function called prod, using reduce. We use list comprehension to convert our list of strings to a list of ints.\n\nNote: In Python 2, reduce is a bu
We define a function called `prod`, using `reduce`. We use list comprehension to convert our list of strings to a list of ints.\n\n**Note**: In Python 2, `reduce` is a built-in function. In Python 3, we need to import the `functools` library. However, in the LeetCode environment, the `functools` library is already included (see: [supported libraries](https://support.leetcode.com/hc/en-us/articles/360011833974-What-are-the-environments-for-the-programming-languages-)).\n\n**Solution**:\n```\nclass Solution:\n    def subtractProductAndSum(self, n):\n        def prod(arr): return reduce(lambda a,b: a * b, arr)\n        x = [int(i) for i in str(n)]\n        return prod(x) - sum(x)\n```\n\nInstead of using `reduce`, we can use prod from the `math` library, and we do not have to define our own `prod` method. This library is automatically included in the LeetCode environment (see: [supported libraries](https://support.leetcode.com/hc/en-us/articles/360011833974-What-are-the-environments-for-the-programming-languages-)).\n\n**Solution**:\n```\nclass Solution:\n    def subtractProductAndSum(self, n):\n        x = [int(i) for i in str(n)]\n        return prod(x) - sum(x)\n```\n\nFor our one-liner, we can use the walrus operator to define `x` and use it on the same line (see: [walrus operator](https://realpython.com/python-walrus-operator/)).\n\n**Solution**:\n```\nclass Solution:\n    def subtractProductAndSum(self, n):\n        return prod(x := [int(i) for i in str(n)]) - sum(x)\n```
['Python', 'Python3']