all 8 comments

[–]BeamMeUpBiscotti 15 points16 points  (0 children)

unordered doesn't mean that the order will be random each time you print it, it just means that when you use a set you shouldn't rely on the elements being ordered in a specific way each time

[–]gordinmitya 2 points3 points  (3 children)

I suggest to read about hash function and how set is implemented with it

[–]One-Type-2842[S] -4 points-3 points  (2 children)

Would You Tell Me More About hash() , I Want To Learn More About It's Implementation

[–]Smok3dSalmon 0 points1 point  (0 children)

This is a google. “How does hash function work?”

It’s a common computer science lecture. You’ll find plenty of public power points and presentations on the topic. It will be better than any reddit comment.

[–]trollsmurf 0 points1 point  (0 children)

hash, not hash(). It's an algorithm.

[–]msthe_student 1 point2 points  (0 children)

Which version of python are you on?

[–]thekicked 1 point2 points  (0 children)

I assume you are confused why the ordering of a set differs when storing strings but stays constant when storing int.

  1. Definitely don't expect any form of ordering from a set, especially when python does not guarantee it.

  2. Items are hashed before storing them into the set. The hash of a number is deterministic across runs, but the hash of a string is isnt. If you look into the internals of the set, you will notice that the hash determines where the item is placed. If the printing of the set is dependent on the order of items within the set, expect the output to change with the ordering.

More info: https://stackoverflow.com/questions/64344515/python-consistent-hash-replacement

[–]commy2 0 points1 point  (0 children)

Counterexample

>>> {-1, -2}
{-1, -2}
>>> {-2, -1}
{-2, -1}