The Python Oracle

Python - Return values from a function

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Life in a Drop

--

Chapters
00:00 Python - Return Values From A Function
00:39 Accepted Answer Score 2
01:01 Answer 2 Score 1
01:14 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)