Python. Challenge. Set. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Python. Challenge. Set.

x = {1: 0, 0: 1} y = [1, [4,6,5,4], 2] print(list(set(y[x[0]]))[1]) Could you explain me why set(y[x[0]]) is always {4, 5, 6})? How can I find a defined order of unordered collection of items?

27th Dec 2018, 10:51 AM
Tsypa
Tsypa - avatar
5 Answers
+ 4
Agreed, it's a really bad challenge question. The order of the elements in a set is supposed to be arbitrary. And even for integers, it is not always in ascending order. Try print(set([1, 544])) for example. I think it's report-worthy.
27th Dec 2018, 11:01 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 5
X[0]=1 Y[1]=[4,6,5,4] Set does not same numbers in it Hence it gives {4,5,6}
27th Dec 2018, 6:49 PM
Mr_¢hito•°•
Mr_¢hito•°• - avatar
+ 1
Kishalaya Saha, thx According to stackoverflow: Sets are usually implemented using hash keys (hash codes). So order is probably related to how hash functions are used instead of natural order of its elements. https://stackoverflow.com/questions/8084825/JUMP_LINK__&&__python__&&__JUMP_LINK-order-in-a-set-of-numbers
27th Dec 2018, 11:05 AM
Tsypa
Tsypa - avatar
0
y[x[0]] x{0:1, 1:0} x[0] => 1 So y[x[0]] => y[1] y[1] => ?
27th Dec 2018, 10:55 AM
Taste
Taste - avatar
0
🇪🇪 Tsypa 🇪🇪 Yes, it probably has something to do with hash values, but it's not completely clear. We had a long and confusing discussion a while ago, if you're interested: https://www.sololearn.com/Discuss/1556797/?ref=app (Please sort the comments by "Date" and not "Votes"). Either way, we should not be expected to know Python's implementation of the hash function, calculate them for those integers, and find the correct result.
27th Dec 2018, 11:11 AM
Kishalaya Saha
Kishalaya Saha - avatar