Python Forum
dictionary and function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: dictionary and function (/thread-17233.html)



dictionary and function - renu6593 - Apr-03-2019

iteration through a dictionary and allocate those values to a function for further calculation

i use python 2.7

x={AB,AD,BD} #Dictionary 1
Y={A=1,B=2,C=3,D=4} #Dictionary 2
Ans = {} #Dictionary 3 , for the answers from the function

def add():

i need to use the add function to add the values according to the sequence i have in the dictionary named X.The values of the variables A,B,C,D are stored in another dictionary named Y.

calculate the addition operation and store it in dictionary named Ans.

kindly Help :)



The dictionary named X having the sequence will have infinite sequence. so i do not want it to be manually allocated to the function.


RE: dictionary and function - perfringo - Apr-03-2019

(Apr-03-2019, 09:54 AM)renu6593 Wrote: i use python 2.7

Don't use Python 2. There is no reason to learn Python other than version 3.

But even in Python 2 I doubt that these are legit dictionaries {AB,AD,BD} and {A=1,B=2,C=3,D=4}


RE: dictionary and function - DeaD_EyE - Apr-03-2019

def foo(data):
    for value in data.values():
        another_function(value)
If you want only the keys, then use .keys().
If you want both, then use .items()

But first you should learn what a dict is.
Your examples are wrong. If the name AB,AD,BD exists, you get a set and not a dict with your first example. Your second dict is an SyntaxError.


Don't use Python 2.7.x.
The support ends 2020 and even the library developers are stopping the support for Python 2.7