-3

I am getting invalid argument error while trying to print dictionary values.

dict={"a":1,"b":2,"c":3}
print(*dict)
print(**dict)

TypeError                                 Traceback (most recent call last)
<ipython-input-49-92f0ed7852d8> in <module>
      1 dict={"a":1,"b":2,"c":3}
      2 print(*dict)
----> 3 print(**dict)

TypeError: 'a' is an invalid keyword argument for print()
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
  • Why are you using `print(**dict)` and not just `print(dict)`? What are you expecting it to do? – khelwood Jan 18 '21 at 08:30
  • 2
    That's not how you print dictionary values. Use `print(*d.values())` instead. Also, do not name your variables same as python built-ins. `dict` is a bad variable name. – Sayandip Dutta Jan 18 '21 at 08:31
  • Does this answer your question? [Passing a dictionary to a function as keyword parameters](https://stackoverflow.com/questions/334655/passing-a-dictionary-to-a-function-as-keyword-parameters) – deadshot Jan 18 '21 at 08:31
  • Thank you Sayandip Dutta for your answer – Jagadeesh Jaga Jan 18 '21 at 09:32

1 Answers1

0

The **dict is used to "Unpacks the contents of a dictionary into the function call, see the reference here:**Dictionary

def add (a=0, b=0, c= 0):
   return a + b + c
dict={"a":1,"b":2,"c":3}
print(*dict)
unpacking = add(**dict)
print(unpacking)

The printed unpacking value will be equal to 6.

You can use dict.values() to get the values of the dictionary. E.g. print (dict.values()) will return

dict_values([1, 2, 3])
XYZ
  • 352
  • 5
  • 19
  • Is there any possible to print just values without open and close braces and parantheis?like it should just print 1,2,3 – Jagadeesh Jaga Jan 18 '21 at 09:16
  • @JagadeeshJaga, Sanandip has already answered. Use `print (*dict.values())` will print only the list values. – XYZ Jan 18 '21 at 09:19