The Python Oracle

Python - Return values from a function

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Light Drops

--

Chapters
00:00 Question
00:54 Accepted answer (Score 2)
01:24 Answer 2 (Score 1)
01:44 Thank you

--

Full question
https://stackoverflow.com/questions/3538...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 2


Use a list comprehension. Change

for (item, value) in config.items(section):
    # the function returns at the end of the 1st iteration
    # hence you get only 1 tuple. 
    # You may also consider using a generator & 'yield'ing the tuples
    return (item, value) 

to

return [(item, value) for item, value in config.items(section)]

And concerning your test() function:

def test():
    aList = getItemsAvailable('test')
    print (aList)



ANSWER 2

Score 1


Use a generator function:

def getItemsAvailable(section):
    for (item, value) in config.items(section):
        yield (item, value)

And get the items like this:

def test():
    for item, value in getItemsAvailable('test'):
        print (item, value)