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
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 579 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
  cannot unpack non-iterable int object in urllib3/util/wait.py", line 85, ping_chen_ibm_us 2 931 Aug-01-2025, 02:05 PM
Last Post: ping_chen_ibm_us
  Replace values in Yaml file with value in dictionary PelleH 1 3,817 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  DEC pack, unpack and disk-images Curbie 32 10,791 Aug-23-2024, 03:37 PM
Last Post: Curbie
Question Using Lists as Dictionary Values bfallert 8 3,937 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  Too much values to unpack actualpy 3 2,143 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  need to compare 2 values in a nested dictionary jss 2 2,974 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 3,856 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  unpack dict menator01 1 2,290 Apr-09-2022, 03:10 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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