-1

I have this part of codeprint(int(input())-int(input())), but i'm need to make my code more short, and i'm looking for a way to do it.

I think, i can do map(int, input.split(' '), but i don't know, how to do difference of two elements of list using functions of Python

Actticus
  • 55
  • 1
  • 6
  • 3
    Why do you need the code to be shorter? You already have a one liner. – Mad Physicist Jan 23 '18 at 19:24
  • 1
    Not sure why you need a `list` when you are just calcualting the difference between two numbers. – Zhiya Jan 23 '18 at 19:25
  • 4
    Don't. Short code is not better. Better code may be longer. To save a penny today, you spend a pound tomorrow. – Dan D. Jan 23 '18 at 19:25
  • Possible duplicate of [Python - Differences between elements of a list](https://stackoverflow.com/questions/2400840/python-differences-between-elements-of-a-list) – yeger Jan 23 '18 at 19:25
  • @yeger: That other question involves taking the difference of *all* consecutive pairs of numbers in a list. The solution there is definite overkill for the simple problem here. Also, the other question assumes the list is formed before the calculation, while in this question the list is formed during the process. – Rory Daulton Jan 23 '18 at 19:28
  • I need it, cause i take part in one competiton, where i must write codу with minimal simbols. In my code 32 simbols. I need 30. Spaces and enters dont count – Actticus Jan 24 '18 at 17:09

1 Answers1

2

You could use operator.sub with starred unpacking of arguments from map

import operator

print(operator.sub(*map(int,"3 1".split())))  # => 2

it's not shorter, but it avoids to access the elements of the splitted list by index, and it's one line.

interactive variant with 2 calls to input() (instead of split on one input):

operator.sub(*(int(input()) for _ in range(2)))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219