Python Forum
How to use value in dictionary as arguments in function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use value in dictionary as arguments in function
#1
def test(data1, data2):
    eef = data1 + data2
    return(eef)

dict1 = {"a": (2, 2),
         "b": (3, 2),
         "c": (5, 6),
         "d": (1, 1)}
            
dict2 = {"f": (3, 1),
         "g": (1, 4)}

foo = test(dict1, dict2)
I want to add the value in dict1 with dict2 using function test but I don't know how to put the value in function
Reply
#2
Create empty dict and call update() on this dict twice to add dict1 and dict2
Reply
#3
I don't want to combine the dictionary, I just want to calculate the sum of value from dict1 and dict2

a:2 + f:3 = 5
a:2 + f:1 = 3
Sorry my English very bad
Reply
#4
why match a and f? before 3.7 dicts are unordered collections, so it's not certain what order you will get...
and what will be the new key? a, f, af, something else...? Again tuple as value?
what about dicts with different len? what about dict values with different len?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
ooh ok so it is not possible to use the tuple inside dictionary for the function
Reply
#6
(Apr-22-2020, 04:46 PM)gabejohnsonny21 Wrote: it is not possible to use the tuple inside dictionary for the function
it is possible, however your "requirements" are unclear - there are huge gray areas about what you want as output and how expect to handle particular cases.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Something like this?
dict1 = {"a": (2, 2),
         "b": (3, 2),
         "c": (5, 6),
         "d": (1, 1)}
             
dict2 = {"f": (3, 1),
         "g": (1, 4)}

def add_it_up(d1, d2):
    d3 = {}
    for a, b in zip(d1.items(), d2.items()):
        a_key, a_values = a
        b_key, b_values = b
        d3[f'{a_key}+{b_key}'] = (a_values[0] + b_values[0], a_values[1] + b_values[1])
    return d3

print(add_it_up(dict1, dict2))
I have no idea what you want for output so I made a new dictionary making keys from the input dictionaries (a+f, b+g).

This would be a little easier using lists and a bit less odd. I know that dictionaries now maintain their order, but I think it strange to think of dictionaries as having order. You use keys in dictionaries. Lists on the other hand can only be referenced using their order. Maybe just a bias I need to get over.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  get method within max function for a dictionary Blanchdog 3 1,869 Jul-03-2020, 05:55 PM
Last Post: ndc85430
  Why is this function printing the number of keys in a dictionary? mafr97 5 3,033 Sep-17-2019, 06:19 AM
Last Post: perfringo
  Function arguments help FingerButton 2 2,824 Oct-23-2017, 09:29 PM
Last Post: FingerButton

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020