Python Forum
Can't unpack values of dictionary with **
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't unpack values of dictionary with **
#1
I've seen similar examples in 4.7.5 Unpacking argument lists, but my somehow don't work.

>>> d={'USA':'Washington','France':'Paris','China':'Beijing'}
	  
>>> **d
	  SyntaxError: invalid syntax
Also:

>>> def f(a,b,c):
	  print(a,b,c)
	  
>>> f(**d)
	  
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    f(**d)
TypeError: f() got an unexpected keyword argument 'USA'
Reply
#2
The keys in the dict need to match the parameter names of the function.
Reply
#3
your function expects only positional arguments and you try to pass keyword arguments it doesn't expect, thus the error. If you pass keyword arguments they need to be present in the function signature. However if keyword arguments present in the function signature you can [almost ever] pass them also as positional arguments (it's possible to force that function take only positional or only keyword arguments). e.g.

def greet(name='John', familly='Doe'):
    print(f'Hello, {name} {familly}')

user = {'name': 'Harry', 'familly': 'Potter'}

greet(**user)
greet(name='Hermione', familly='Granger')
greet('Ron', 'Weasley')
greet()
Output:
Hello, Harry Potter Hello, Hermione Granger Hello, Ron Weasley Hello, John Doe
In addition, your use case does not really warrant use of multiple keyword arguments. Do you really want to have close to 200 arguments? It would be better to pass the dict as single argument

def print_capitals(capitals=None):
    if capitals:
        for country, capital in capitals.items():
            print(f'Capital of {country} is {capital}')
    else:
        print("Youd didn't supply capitals")

my_dict = {'USA':'Washington','France':'Paris','China':'Beijing'}
print_capitals()
print_capitals(capitals=my_dict)
Output:
Youd didn't supply capitals Capital of USA is Washington Capital of France is Paris Capital of China is Beijing
Now, there is a way to take arbitrary number of positional and keyword arguments.
def some_func(*args, **kwargs):
    print(f'Positional arguments: {args}')
    print(f'Keyword arguments: {kwargs}')
    print('--------------\n')


pos = {'foo', 'bar'}
keyw = {'spam':1, 'eggs':2}

some_func('foo', spam=1)
some_func('foo', 'bar', spam=1, eggs=2)
some_func(*pos)
some_func(**keyw)
some_func('foo', **keyw)
some_func(*pos, **keyw)
Output:
Positional arguments: ('foo',) Keyword arguments: {'spam': 1} -------------- Positional arguments: ('foo', 'bar') Keyword arguments: {'spam': 1, 'eggs': 2} -------------- Positional arguments: ('foo', 'bar') Keyword arguments: {} -------------- Positional arguments: () Keyword arguments: {'spam': 1, 'eggs': 2} -------------- Positional arguments: ('foo',) Keyword arguments: {'spam': 1, 'eggs': 2} -------------- Positional arguments: ('foo', 'bar') Keyword arguments: {'spam': 1, 'eggs': 2} --------------
Note, args and kwargs are just names used by convention, they can be different
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Got it. Thanks guys.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Too much values to unpack actualpy 3 408 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  need to compare 2 values in a nested dictionary jss 2 797 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 1,316 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  unpack dict menator01 1 1,158 Apr-09-2022, 03:10 PM
Last Post: menator01
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 6,277 Jan-28-2022, 06:36 PM
Last Post: deanhystad
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,695 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Getting values from a dictionary brunolelli 5 3,516 Mar-31-2021, 11:57 PM
Last Post: snippsat
  [SOLVED] [geopy] "ValueError: too many values to unpack (expected 2)" Winfried 2 2,835 Mar-30-2021, 07:01 PM
Last Post: Winfried
  Python dictionary with values as list to CSV Sritej26 4 2,956 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,903 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020