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


Messages In This Thread
Unpacking a dict with * or ** - by msrk - Nov-30-2023, 02:40 PM
RE: Unpacking a dict with * or ** - by Gribouillis - Nov-30-2023, 02:51 PM
RE: Unpacking a dict with * or ** - by msrk - Dec-02-2023, 11:50 PM
RE: Unpacking a dict with * or ** - by deanhystad - Nov-30-2023, 05:45 PM
RE: Unpacking a dict with * or ** - by rob101 - Nov-30-2023, 05:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 2,601 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  Unpacking zip object Mark17 12 6,938 Mar-28-2022, 04:59 PM
Last Post: deanhystad
  unpacking list wardancer84 2 2,679 Sep-11-2021, 02:42 PM
Last Post: wardancer84
  unpacking tuple not working project_science 1 2,545 Jan-09-2021, 09:09 PM
Last Post: buran
  Unpacking a list Mark17 3 3,822 Dec-18-2020, 05:08 AM
Last Post: aajkaal
  Unpacking wheel files DavidTheGrockle 3 32,141 Dec-15-2020, 05:11 PM
Last Post: DavidTheGrockle
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,937 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  Unpacking nested lists yonatan776 1 2,916 Apr-14-2020, 08:50 PM
Last Post: buran
  Sort a dict in dict cherry_cherry 4 104,623 Apr-08-2020, 12:25 PM
Last Post: perfringo
  Unpacking dictionary from .xlsx as Kwargs etjkai 5 4,159 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