1

Imagine we have a dictionary

views_params = {"list_a": [1,2,3], "list_b": [1,2,8]}

I do want to iterate in each list of values by key using the following function:

def function_with_listed_arguments(k, y, z):
    w = k + y + 2*z
    return w 

I tried using a dictionary comprehension to get key:value pairs:

dictionary_list = {
    k: function_with_listed_arguments(v) for k,v in views_params
}
final_result = [dictionary_list.values()]
print(final_result)

The final result should be [9,19]

However, I am getting ValueError: too many values to unpack (expected 2) in the dictionary comprehension. Anybody can point me to what am I doing wrong?

Tried with **kwargs still no results.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
DonCharlie
  • 53
  • 7

1 Answers1

2

You were almost there! You only needed items() on the dict and a list decomposition when passing the params.

views_params = { "list_a": [1,2,3], "list_b": [1,2,8],}

def function_with_listed_arguments(k,y,z):
  w = k + y + 2*z
  return w

dictionary_list = {
    k: function_with_listed_arguments(*v) for k,v in views_params.items()
}
final_result = [dictionary_list.values()]
print(final_result)

Outcome:

[dict_values([9, 19])]
Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74