Python sets: UNORDERED - or not?!?! | SoloLearn: Learn to code for FREE!

+12

# Python sets: UNORDERED - or not?!?!

x = {1:0, 0:1} y = [1, [4,6,5,4], 2] print(list(set(y[x[0]]))[1]) This is a SL challenge with the official solution of 5. How can it be 5 if sets are unordered?

2/19/2020 2:09:33 PM

Prof. Dr. Zoltán Vass

+11

Guys, everyone is right ..😨 It can be ordered, it can be unordered, it can be sorted https://code.sololearn.com/c3NmGRKpq0ye/?ref=app

+11

Summary is short: A set is unordered. The function set() does not sort. set() might return an ordered set but only by luck. Gordon is a lucky guy. Geeks for geeks is buggy at this point.

+8

edit: this answer is wrong, just keeping to keep the whole conversation makes sense. The datatype set is unordered but the function set is ordering them https://www.google.com/amp/s/www.geeksforgeeks.org/python-set-method/amp/ set() method is used to convert any of the iterable to the distinct element and sorted sequence of iterable elements, commonly called Set. that's why set([4,6,5,4]) always returns {4,5,6}

+8

Prof. Dr. Zoltán Vass I have seen that example. The output is sorted in that example but not in my example. Why? if set() function always sorts? It does not always sorts. The elements are printed in order they exist in memory which is determined based on hashing function.

+8

Gordon s text was not official docu but geeks for geeks https://www.geeksforgeeks.org/python-set-method/ which normally has high quality and can be trusted. The official docu doesn't mention set() will sort. But I play around with Gordons code... shuffle list by hand... it keeps sorted. EXCITING!

+8

~ swim ~ Gordon should play Bingo or any other lottery. seems to have luck

+7

Ipang CPython (considered as reference implementation) interpreter is written in C PyPy is JIT Compiled and is written in Python. It's actually based on RPython IronPython is written in .NET Python in general is Byte Compiled Interpreted (as in CPython) or JIT Compiled(as in PyPy) language

+7

So it seems that this challenge question needs to be reported as invalid then....

+6

edit: this answer is wrong, just keeping to keep the whole conversation makes sense. Prof. Dr. Zoltán Vass It's still a sorted set ~ https://code.sololearn.com/c1QQSpE9kF5o/?ref=app Refer to Python Doc https://docs.python.org/3.8/library/stdtypes.html#set `Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.` In short, we shouldn't use common sense of 'unordered' == 'unsorted' to interpret programming language. 'Unordered' only means you couldn't use index to access its element.

+6

~ swim ~ we have the object Set and the function set() last one sorts the elements.

+6

Prof. Dr. Zoltán Vass GeeksforGeeks is a very good site but not official documentation. You may run my code, the result is in front of you :)

+6

Gordon is right but I'll like to add a little clarification . Set object is unordered and the function is ordered. If something is ordered in python, it means the start arrangement won't be sorted. For example s = {"c":15, "a":10 }. // this will always remain as it is so it is referred to as ordered But set({"c", 15, "a", 10}) will change and arrange its items in a lexicographic manner so it will be sorted ... Sorted is different from ordered

+6

one more... can we change Gordons example... simply by changing numbers to stop that misleading output?

+6

I ran Gordon's code and it seemed to sort. But then I made a couple of changes, and the result was not sorted. a = [24, 6, 35, 4] print(set(a)) import random b = [1, 2, 3, 4, 35, 6, 7, 8, 9, 10] random.shuffle(b) print(b) print(set(b)) Try it

+6

I tested it. Some interpreters output unsorted and some other sorted. No matter if set () function is used or not. Unorderd, unindexed and unsorted must be discussed separately.

+6

So don’t trust in set() method in producing sorted output, even if certain sources suggest it. In this case, the official solution of the SL challenge cited in my original question should be wrong.

+6

Prof. Dr. Zoltán Vass The theory of hashing function is obscured to me as well. I only have a rough idea that it choose X bits from elements (size of X depends on size of set) the hashing function then use those X bits to determine where the elements should go in the table.

+5

Oma Falk I am using set() function. It is not sorting. Am I missing something?

+5

Oma Falk That is what i am trying to say that it may not be always sorted. In Gordon's example the hash function produce a sorted list.

+5

~ swim ~ Perhaps your insight is the solution: “The elements are printed in order they exist in memory which is determined based on hashing function.” Only the theory of hashing function is a little obscure for me here :)