0

I have a function :

 def getvalues():
       global avalue
       global bvalue
       dictt = getresults()
       for key , value in dictt.iteritems():
            avalue = key
            bvalue = dictt[key]
            print avalue , bvalue

This prints the value of avalue , bvalue Output:

C123 1
C245 2
C456 2
C565 3
C654 1

but , returning the values outside for loop doesn't iterate over.Like this:

 def getvalues():
       global avalue
       global bvalue
       dictt = getresults()
       for key , value in dictt.iteritems():
            avalue = key
            bvalue = dictt[key]
       return avalue , bvalue

I need avalue , bvalue to be used outside this function.How can this be done?? If i use return i get the output as

C654 1

I need the same output of above with all avalue , bvalue that i can use in other function

pri_newbie
  • 63
  • 1
  • 2
  • 6
  • 1
    You can use `list` to append say `avalue = [], bvalue= []` and use like `avalue.append(key) , bvalue.append(getvalues[key])` return these and you can iterate over them use outside function – Nikhil Parmar Jan 29 '16 at 05:26
  • Not sure what you want, but this could be a case for a [generator with `yield`](https://wiki.python.org/moin/Generators) (third code sample). –  Jan 29 '16 at 05:28
  • 3
    Your code makes little sense; it calls itself until Python runs out of stack, and it seems to confuse the name of the function with the name of the dictionary. Please post code that actually runs and does what you say it does. – kindall Jan 29 '16 at 05:28
  • corrected the sample code differentiating function name and dictionary name – pri_newbie Jan 29 '16 at 05:53

1 Answers1

9
def getvalues():
    values = []
    dictt = getvalues()
    for key , value in getvalues.iteritems():
         values.append((key, getvalues[key])) 
    return values
John Howard
  • 61,037
  • 23
  • 50
  • 66
Steve Hostetler
  • 346
  • 2
  • 12
  • append() takes only one argument – pri_newbie Jan 29 '16 at 05:53
  • he is appending a tuple. that is one argument. Notice how it's values.append( (key, getvalues[key]) ). So you end up with a list of tuples. Each item in that list is a tuple with two values. Unless this was later added in the edit, not sure if You noticed it. – Amit Jan 29 '16 at 07:03