Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dictionary and function
#1
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.
Reply
#2
(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}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  passing dictionary to the function mark588 2 972 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  Writing a lambda function that sorts dictionary GJG 1 2,015 Mar-09-2021, 06:44 PM
Last Post: buran
  Using Dictionary to Test Evenness of Distribution Generated by Randint Function new_coder_231013 6 3,257 Feb-23-2021, 01:29 PM
Last Post: new_coder_231013
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 3,206 May-11-2019, 03:18 AM
Last Post: keames
  Why does my code not execute? (def function with 'for in' iteration over a dictionary Placebo 3 2,924 Oct-19-2018, 01:32 PM
Last Post: Placebo
  calling a function which takes a dictionary as argument Annie 4 4,552 Jan-04-2017, 07:42 AM
Last Post: Annie
  Python help with module function return dictionary tebirkes 14 15,807 Oct-15-2016, 11:45 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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