Python Forum
Unpacking a dict with * or **
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unpacking a dict with * or **
#1
I can unpack a dict with the ** operator. But something strange happens if I try to unpack it with *. This may be obvious given that the * operator unpacks a tuple or list. But watch this:

x = {'a': 7, 'b': 8}
print(*x)


That works and the output is "a b" but this does not:

z = *x
print(z)


So, in the first case when print is called it seems the def print(*args) has the effect of **x but how and why if the second example does not work? Why doesn't the code first execute *x and then pass the result to print()? If it could do that then why doesn't the second example work?
Reply
#2
*x is not a valid Python expression, it cannot be evaluated. What would its value be? On the other hand func(*x) is a valid expression. There is no unary operator * or **.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
*x unpacks x into two objects, 'a' and 'b'. This works fine when used in a function call. print(*x) becomes print('a', 'b'). This cannot work here:
z = *x
z can reference one object, but *x returns two. To get the two objects you need to use two variables and unpack like this:
y, z = x
Or you could unzip into a container like this:
z = (*x,)
print(z)
Output:
('a', 'b')
Reply
#4
I'm sure there's a name for this, but I'm unsure what that name is, but you can use a * in the print() call, ahead of an iterative object. I use it for displaying the contents of a list object, for one. As an example: rather than print(lst) which displays the 'object', I'll sometimes use print(*lst) which displays the items.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
(Nov-30-2023, 02:51 PM)Gribouillis Wrote: *x is not a valid Python expression, it cannot be evaluated. What would its value be? On the other hand func(*x) is a valid expression. There is no unary operator * or **.

Thank you. I originally thought it was a unary operator that would unpack x. But then that unary operator would be a function and functions return scalars or tuples, a pack not unpack of results. If not a unary operator than what? Your response made me think. It seems like * and ** are part of the language syntax and the language parser treats them like an operator in certain cases. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,315 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  Unpacking zip object Mark17 12 3,246 Mar-28-2022, 04:59 PM
Last Post: deanhystad
  unpacking list wardancer84 2 1,894 Sep-11-2021, 02:42 PM
Last Post: wardancer84
  unpacking tuple not working project_science 1 1,497 Jan-09-2021, 09:09 PM
Last Post: buran
  Unpacking a list Mark17 3 2,629 Dec-18-2020, 05:08 AM
Last Post: aajkaal
  Unpacking wheel files DavidTheGrockle 3 11,442 Dec-15-2020, 05:11 PM
Last Post: DavidTheGrockle
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,078 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  Unpacking nested lists yonatan776 1 2,214 Apr-14-2020, 08:50 PM
Last Post: buran
  Sort a dict in dict cherry_cherry 4 75,735 Apr-08-2020, 12:25 PM
Last Post: perfringo
  Unpacking dictionary from .xlsx as Kwargs etjkai 5 2,865 Dec-27-2019, 05:31 PM
Last Post: etjkai

Forum Jump:

User Panel Messages

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