4

I have this config file:

[test]
one: value1
two: value2

This function return items, values from section test of config file, but when I call the function only return first item (one, value1).

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

I call the getItemsAvailable() with this function:

def test():
    item, value = getItemsAvailable('test')
    print (item, value)

I guess that I should create a list on getItemsAvailable() function, and return the list for read the values on test() function, true?

Any suggestions?

Thanks!!

0

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, very nice solution. Thanks!! ;)
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)

1 Comment

Wow, I think that I will write this on my code. Tanks!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.