When we convert a list into a set, most of the time the elements in the list just shuffle up and be in a random manner. I just want to know how it happens while converting a list into a set. I here know that it excludes all the duplicates from the list and leave only one behind.
Asked
Active
Viewed 477 times
1 Answers
1
Sets in Python don't have a defined order (they are explicitly unordered). Implementation-wise they are more or less the same as dictionaries and are hash-sets; converting from a list to a set would pretty much be a for-loop inserting all elements of the list to the set. See this question for more details.

CoconutFred
- 454
- 3
- 11
-
1Note: The "explicitly unordered" bit (really, "arbitrarily ordered") is true of `set`s, but not `dict`s (which iterate in insertion order as an implementation detail of CPython 3.6, and a language guarantee as of 3.7), which makes your analogy to `dict`s somewhat misleading. – ShadowRanger Jul 06 '19 at 04:37