### python

#### How can I compare the difference between digits of two numbers?

```For eg I have two numbers:4532 and 5489.I want to calculate the least difference between first digit of 4532 which is 4 and all the digits of 5489 ie 4-5, 4-4, 4-8 and 4-9.How can I do it?
num = "4532"
num2 = "5489"
for j in num2:
if int(i)-int(j)<int(i)-int(j+1):
print(int(i)-int(j)) #I am getting list index out of bound exception
```
```It's not a nice code, but it's working. You can adjust this to what you want.
Keep in mind that if you have two digits with the same result you wouldn't know that (you'll be left with the first who matched, in that case)
In that example you'll get the digit that will give you the least difference. You can change it to save the result instead.
num1 = 4532
num2 = 5489
ln2 = len(str(num2))
firstDigit = int(str(num1)[0])
lowest = None
temp = num2
for i in range(ln2):
last = temp % 10
temp //= 10
diff = abs(firstDigit - last)
if lowest is None or diff < abs(lowest - firstDigit):
lowest = last
print(lowest)```

### Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization