Python Forum
Why the result of "extended iterable unpacking" with set() is unpredictable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why the result of "extended iterable unpacking" with set() is unpredictable?
#1
Hello everyone,
I want to test "extended iterable unpacking" with set, but the result is unpredictable.

d1, *d2, d3 = {1, 2, 3, "a", "acc"}
print("d1 =", d1, ", d2 =", d2, ", d3 =", d3)
print("type(d1) =", type(d1), ", type(d2) =", type(d2), ", type(d3) =", type(d3))
I can get different results as below
Quote:d1 = 1 , d2 = [2, 3, 'acc'] , d3 = a
type(d1) = <class 'int'> , type(d2) = <class 'list'> , type(d3) = <class 'str'>

Quote:d1 = 1 , d2 = [2, 3, 'a'] , d3 = acc
type(d1) = <class 'int'> , type(d2) = <class 'list'> , type(d3) = <class 'str'>

Quote:d1 = acc , d2 = [1, 2, 3] , d3 = a
type(d1) = <class 'str'> , type(d2) = <class 'list'> , type(d3) = <class 'str'>

Could somebody tell me what the reason is?
Reply
#2
because set is unordered collection by definition :-)
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
#3
I understand what you mean.
Thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unpacking a dict with * or ** msrk 4 915 Dec-02-2023, 11:50 PM
Last Post: msrk
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,262 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 1,521 May-24-2022, 12:03 PM
Last Post: deanhystad
  Unpacking zip object Mark17 12 3,106 Mar-28-2022, 04:59 PM
Last Post: deanhystad
  unpacking list wardancer84 2 1,837 Sep-11-2021, 02:42 PM
Last Post: wardancer84
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,095 Aug-10-2021, 08:10 AM
Last Post: snippsat
  unpacking tuple not working project_science 1 1,451 Jan-09-2021, 09:09 PM
Last Post: buran
  Unpacking a list Mark17 3 2,555 Dec-18-2020, 05:08 AM
Last Post: aajkaal
  Unpacking wheel files DavidTheGrockle 3 10,881 Dec-15-2020, 05:11 PM
Last Post: DavidTheGrockle
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 8,817 Oct-18-2020, 02:11 PM
Last Post: Jadiac

Forum Jump:

User Panel Messages

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