In python, set() is an unordered collection with no duplicate elements. However, I am not able to understand how it generates the output.

For example, consider the following:

```>>> x = [1, 1, 2, 2, 2, 2, 2, 3, 3]
>>> set(x)
set([1, 2, 3])

>>> y = [1, 1, 6, 6, 6, 6, 6, 8, 8]
>>> set(y)
set([8, 1, 6])

>>> z = [1, 1, 6, 6, 6, 6, 6, 7, 7]
>>> set(z)
set([1, 6, 7])```

Shouldn't the output of set(y) be: set([1, 6, 8])? I tried the above two in Python 2.6.

Oct 17, 2018 in Python 421 views

## 1 answer to this question.

Sets are unordered, as you say. Even though one way to implement sets is using a tree, they can also be implemented using a hash table (meaning getting the keys in sorted order may not be that trivial).

If you'd like to sort them, you can simply perform:

`sorted(set(y))`

which will produce a sorted list containing the set's elements. (Not a set. Again, sets are unordered.)

Otherwise, the only thing guaranteed by set is that it makes the elements unique (nothing will be there more than once).

• 58,090 points

## .join() method is used for what purpose in Python?

join() method is used to concatenate or ...READ MORE

## assert keyword in python is used for what purpose?

The assert keyword is used while debugging ...READ MORE

## What is the patterns package in python used for?

Pattern is a web mining tool in ...READ MORE

## What is `related_name` used for in Django?

Hello @kartik, The related_name attribute specifies the name of the ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

## Lowercase in Python

You can simply the built-in function in ...READ MORE

+1 vote

## What is the function for Factorial in Python

Easiest way: math.factorial(x) (available in 2.6 and ...READ MORE

## What is the python keyword “with” used for?

The with statement in Python simplifies exception ...READ MORE