0

For example I have a function

def function_1():
    x = 1
    y = 2
    z = 3
    return x, y, z

which I want to call and store it a dictionary in another function

def function_2():
    dict = {}
    for i in range(10):
        dict[i] = function_1(), i

what I imagined the code to give me was a dictionary with keys like this:

{0: (1, 2, 3, 0), 1: (1, 2, 3, 1), 2: (1, 2, 3, 2)}.

But what I get instead is

{0: ((1, 2, 3), 0), 1: ((1, 2, 3), 1), 2: ((1, 2, 3), 2)}.

Now what I would like to get is the aforementioned format for every key. How can I go about and do that?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • `function_1(), i` is a tuple with the result of `function_1` as the first element and `i` as the second. If you want to *append* to the tuple returned from `function_1`, do `function_1() + (i,)`. – deceze Mar 27 '20 at 11:12

1 Answers1

0

Use tuple unpacking like below:

dict[i] = *function_1(), i

Code:

def function_2():
    dict = {}
    for i in range(10):
        dict[i] = *function_1(), i

Note: Please don't name your dictionary as dict.

Austin
  • 25,759
  • 4
  • 25
  • 48