-5

I would like to iterate through the key:value pairs in a dictionary and pass them as arguments in a function. In the function I am trying to return the value I am getting from an API call. My ultimate goal is to set the keys equal to the value I am getting from the API call.

def SomeFunction(key,val):
    return APICall(val)


myDict = {'a' : 'A',
          'b' : 'B',
          'c' : 'C'
         }
for key,val in myDict.items():
    key = SomeFunction(key,val)

print a
#>>>'return value from API'

When I run this I get an error saying 'a' is not defined. I am struggling to understand why

key = SomeFunction(key,val)

does not instantiate the given key with the assignment of the return value from the API call.

Any help would be great.

alexjones
  • 86
  • 1
  • 9
  • To assign/change value for a key of a dict, you have to do `dict[key] = value`. – Haris Jun 08 '17 at 13:48
  • 2
    What is `a`...? – cs95 Jun 08 '17 at 13:48
  • @Shiva 'a' is the first key in the dictionary. I wish to create a variable 'a' and assign the return value from an API call. – alexjones Jun 08 '17 at 13:51
  • @Haris I am not looking to to manipulate the dictionary. I want to create variables with the same names as the keys in the dictionary and assign the API return values to them. – alexjones Jun 08 '17 at 13:53
  • 1
    Why do you want to "create variables with the same names as the keys in the dictionary" ? It's possible to do that, but in generally it's a _really bad_ idea to do so. Just use another dictionary if you don't want to mutate the original `myDict`. – PM 2Ring Jun 08 '17 at 13:55
  • 1
    @PeterHerrick You want to create variables during runtime, according to the contents of the dictionary.. Not a good idea.. – Haris Jun 08 '17 at 13:55
  • @PM2Ring I was just hoping to be able to avoid creating another dictionary due to the fact that elsewhere in my script I am looping through highly nested dictionaries with these values and did not want to add to the clutter. – alexjones Jun 08 '17 at 14:00
  • Take a look at [How do I create a variable number of variables?](https://stackoverflow.com/q/1373164/4014959). As you can see, the top answers recommend using a dict, or object attributes. Nadia's answer does show how you can dynamically create variables by manhandling the `globals()` dictionary, but that's really not a good idea: it only gives you global variables, not locals, and you're still using a dictionary, so you might as well use one that you have full control over, rather than messing with an essential program control object like the `globals()` dict. – PM 2Ring Jun 08 '17 at 14:07
  • BTW, since you were puzzled that you got an error message saying 'a' is not defined, you may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Jun 08 '17 at 14:16
  • 2
    Possible duplicate? [Python: include entries of a dictionary in the local namespace of a function](https://stackoverflow.com/q/21782132/674039) – wim Jun 08 '17 at 16:38

2 Answers2

0

I don't think you need to send key to SomeFunction(), since only val is used by the API call

Plus, to assign/change value for a key in a dict you have to use dict[key] == value.

So your code should change to..

def SomeFunction(key,val):
    return APICall(val)


myDict = {'a' : 'A',
          'b' : 'B',
          'c' : 'C'
         }
for key,val in myDict.items():
    dict[key] = SomeFunction(val)

Now to print the return value from the API call, (like you were trying for a), you would need to iterate through the dict again and print the keys and values.

Haris
  • 12,120
  • 6
  • 43
  • 70
0

If you want to change the value of a key in a dctionary, you need to re-assign it, as @Haris has already pointed out.

for key,val in myDict.items():
    myDict[key] = SomeFunction(key,val)

print myDict['a']

Note that you can not just use print a, because you do not magically get a variable a - the string a is still just a key in your dictionary.

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50