0

I used a for loop to construct a dictionary:

dict = {}

for i in np.arange(0, n):
    dict[i] = some_data[i]

Then I want to pass the items of the dictionary to a function:

function(dict[0], dict[1], dict[2], ... dict[n])

As n tends to vary between different datasets, is there a way for me to pass multiple inputs to the function without having to manually input dict[0] to dict[n] every time?

I tried to construct an array of [dict[0], dict[1], ..., dict[n]] but the function did not accept an array as the input.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Tran Tran
  • 3
  • 1
  • 1
    Does this answer your question? [What does \*\* (double star/asterisk) and \* (star/asterisk) do for parameters?](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) – Carson Jun 11 '20 at 05:26
  • Yes! This and Grismar's reply answered my question perfectly. Thanks a lot! – Tran Tran Jun 11 '20 at 08:53

1 Answers1

1

Yes you can, it's called spreading:

some_data = [4, 5, 6]

# here's another trick:
d = dict(enumerate(some_data))
print(d)


# some function that takes multiple arguments
def f(*args):
    print(*args)


# calling such a function with the values of your dictionary, as requested
f(*d.values())

Result:

{0: 4, 1: 5, 2: 6}
4 5 6
Grismar
  • 27,561
  • 4
  • 31
  • 54