-1

I currently have a dictionary and a list, and I would like to subtract the list values from the respective dictionary values (based on index position). This is what I have tried so far:

dict1 = {
    "appel": 3962.00,
    "waspeen": 5018.75,
    "ananas": 3488.16
}

number_fruit = [
    3962,
    5018.74,
    3488.17
]

dictionary_values = list(number_fruit)
list_values = list(dict1.values())

are_values_equal = dictionary_values == list_values

But this only returns True or False if there is a difference.

But what I want to achieve is to return the difference between number_fruit list and dict1 dictionary, only if the difference is not equal to zero..

Here is the expected output:

waspeen : 0.01
ananas : -0.01
ScottC
  • 3,941
  • 1
  • 6
  • 20
mightycode Newton
  • 3,229
  • 3
  • 28
  • 54
  • Can you explain why you have the `dictionary_values` equal to `list(number_fruit)` and then `list_values` equal to `dict.values()` ? Seems to me these should be the other way around ? – ScottC Dec 13 '22 at 12:32
  • @ScottC. I just need to compare the values between a dicionary and the values from a list. And then show the values that difference. – mightycode Newton Dec 13 '22 at 12:33
  • I understand, but this is a good way to confuse people who read your code. – ScottC Dec 13 '22 at 12:34
  • 1
    The same comment as the last time you posted this: dicts don't *necessarily* have an order (only within certain limitations). Are you aware of that? Can you guarantee you fall within these specific limitations? Otherwise there's no safe way to associate `dict1['waspeen']` and `number_fruit[1]`…! – deceze Dec 13 '22 at 12:49
  • @deceze. But it is in order. But I don't understand you . Because I googled a lot. And I see posts where other users ask for comparing dict with list, for example this one: https://stackoverflow.com/questions/47554060/python-compare-list-elements-with-dictionary-elements – mightycode Newton Dec 13 '22 at 12:56
  • @deceze. Oke, but what do you then suggest, so that I can compare this values? – mightycode Newton Dec 13 '22 at 12:57
  • See https://stackoverflow.com/a/39980744/476. – deceze Dec 13 '22 at 12:58
  • @deceze. I extract some data from a pdf file and I subtract some data from excel from. And then I compare them. So the outcome is always the same. – mightycode Newton Dec 13 '22 at 13:01
  • If you're extracting the data and aren't given a dict per se, then choose a more appropriate data structure that doesn't have the asterisks about ordering. Or extract your Excel data as a dict as well to make them match. – deceze Dec 13 '22 at 13:02
  • @deceze I extract only numbers from the PDF. So that is a list. And I do a sum for the excel values - so that is a dictionary. Like this: fruit_sums = { "Watermeloen": 0, "Appel": 0, "Sinaasappel": 0, } So the order is always fixed. So I understand that the output of a dict in python is not in order. But the data that is genereated is always in order. so it will work. – mightycode Newton Dec 13 '22 at 13:04

2 Answers2

1

As mentioned by deceze - dicts do not necessarily have an order, and as such, comparing the values is dangerous.

Here is my attempt at this :

Code:

dict1 = {"appel": 3962.00, "waspeen": 5018.75, "ananas": 3488.16}
number_fruit = [3962, 5018.74, 3488.17]

for k, v in dict1.items():
    dict_index = list(dict1.keys()).index(k)
    if (v - number_fruit[dict_index] != 0):
        print(f"{k} : {round(v - number_fruit[dict_index], 2)}")

Output:

waspeen : 0.01
ananas : -0.01
ScottC
  • 3,941
  • 1
  • 6
  • 20
1

Here is a simple solution:

dict1 = {
    "appel": 3962.00,
    "waspeen": 5018.75,
    "ananas": 3488.16
}

number_fruit = [
    3962,
    5018.74,
    3488.17
]

for fruit, a, b in [
    (fruit, value, number_fruit[i])
    for i, (fruit, value) in enumerate(dict1.items())
]:
    print(f'{fruit} : {a} - {b} = {a - b:0.2f}')

Resulting in:

appel : 3962.0 - 3962 = 0.00
waspeen : 5018.75 - 5018.74 = 0.01
ananas : 3488.16 - 3488.17 = -0.01

And it can be even more simple with zip():

for ((fruit, a), b) in zip(dict1.items(), number_fruit):
    print(f'{fruit} : {a} - {b} = {a - b:0.2f}')

And to have just those items whose differences are not 0, you can add an if statement:

for ((fruit, a), b) in zip(dict1.items(), number_fruit):
    if (a - b):
        print(f'{fruit} : {a} - {b} = {a - b:0.2f}')

Resulting in:

waspeen : 5018.75 - 5018.74 = 0.01
ananas : 3488.16 - 3488.17 = -0.01
accdias
  • 5,160
  • 3
  • 19
  • 31