Skip to content Skip to sidebar Skip to footer

How To Append The Second Return Value, Directly To A List, In Python

If a function returns two values, how do you append the second value directly to a list, from the function result? Something like this: def get_stuff(): return 'a string', [1,2

Solution 1:

You can index a tuple using the same indexing you would for a list []. So if you want the list, which is the second element, you can just index the element [1] from the return of the function call.

def get_stuff():
    return 'a string', [1,2,3,5]

all_stuff = [6,7]
all_stuff.extend(get_stuff()[1])

Output

[6, 7, 1, 2, 3, 5]

Solution 2:

Post a Comment for "How To Append The Second Return Value, Directly To A List, In Python"