Why the set1 is sorted and set2 no? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4
28th Sep 2018, 9:29 AM
Seyed Ali Aletaha
Seyed Ali Aletaha - avatar
3 Answers
+ 8
https://docs.python.org/3.7/tutorial/datastructures.html#sets
28th Sep 2018, 7:02 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 2
Both sets are unsorted (even if it looks differently). Sets are unordered collection of unique elements and cannot be indexed too. To get set sorted you should use: sorted(), but this function convert set to list. set1 = {3, 9, 5, 7} set2 = sorted(set1) print (set1, type (set1)) print (set2, type (set2)) #output: # {9, 3, 5, 7} <class 'set'> # [3, 5, 7, 9] <class 'list'> https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2464/
28th Sep 2018, 7:27 PM
blackfish
blackfish - avatar
+ 2
In the first case (set1), the so-called sorting is an accident. Because out of the definition of a set it is a "container" containing non-repeatable elements (unique) in a random order. As can be seen from the second case (set2) here are the elements of the set, already in random order. (As it should be). There is nothing scary about this, it's a set) It's a variable data type and almost similar to the list of functions: 1. add elements (add) 2. delete (pop) 3. concatenate, subtract, multiply, divide, etc .
29th Sep 2018, 10:26 AM
Amir
Amir - avatar