1

I am returning several values in a function:

def count_chars(e):
    return len(e), 'bar'

like this:

for d in lst:
    newlst = []
    for x in d["data"]:
        newlst.extend([x, count_chars(x)])
        d["data"] = newlst
pprint(lst)

However, when I return the values the come inside a tuple:

{'data': ['YES', (9, 'bar')], 'info': 'AKP'}

How can I get rid of the tuple? for

{'data': ['YES', 9, 'bar'], 'info': 'AKP'}
2
  • 2
    What are you passing in lst? Commented Nov 29, 2018 at 16:08
  • a list of dicts @dataLeo Commented Nov 29, 2018 at 16:09

1 Answer 1

5

Unpack the function result (which is a tuple) with the * operator:

newlst.extend([x, *count_chars(x)])

That syntax is only available in Python >= 3.5. Otherwise you can use simple concatenation:

newlst.extend([x] + list(count_chars(x)))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.