Why 40 is the 5th number of this set in the below code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why 40 is the 5th number of this set in the below code?

https://code.sololearn.com/cA59A15A4A17/#py

26th Jan 2021, 2:36 PM
The future is now thanks to science
The future is now thanks to science - avatar
5 Answers
+ 2
set is unordered, therefore the index of 40 has no meaning. why does 40 happen to be 5th? Python knows... Python somehow decides it "under the hood". it is not guaranteed that the set elements will have the same output order when we run it somewhere else. I suppose it is similar to dict in older Python version (in the latest version key-order is preserved)
26th Jan 2021, 4:46 PM
Lisa
Lisa - avatar
+ 2
Thanks Lisa for your explanation but is there any way to sort a set in python?
27th Jan 2021, 3:00 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
The link took me to this question only .
26th Jan 2021, 2:38 PM
Abhay
Abhay - avatar
+ 1
The future is now thanks to science Haven't used it myself, but maybe this can help you: https://pypi.org/project/ordered-set/
27th Jan 2021, 3:03 PM
Lisa
Lisa - avatar
0
Sorry Abhay .That was a network issue, changed it
26th Jan 2021, 2:38 PM
The future is now thanks to science
The future is now thanks to science - avatar