Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Defining a Set in Python

Johnny Swanny on March 14, 2020

Other tutorials I’ve watched on lists/sets never mentioned anything about hashes. Thanks for covering them!

kiran on Aug. 1, 2020

Why set showing same order?

>>> people = {"Jay", "Idrish", "Archi"}
>>> people
{'Idrish', 'Archi', 'Jay'}
>>> people
{'Idrish', 'Archi', 'Jay'}
>>> people
{'Idrish', 'Archi', 'Jay'}
>>> people
{'Idrish', 'Archi', 'Jay'}
>>> people
{'Idrish', 'Archi', 'Jay'}
>>> people
{'Idrish', 'Archi', 'Jay'}
>>> people
{'Idrish', 'Archi', 'Jay'}
>>> people
{'Idrish', 'Archi', 'Jay'}

Bartosz Zaczyński RP Team on Aug. 3, 2020

It’s an implementation detail. You should assume that elements of a set can appear in any order.

James Uejio RP Team on Aug. 4, 2020

Weirdly enough it comes out in a different order on my computer…

In [5]: {'Idrish', 'Archi', 'Jay'}                                              
Out[5]: {'Archi', 'Idrish', 'Jay'}

Also on Python 2.7

>>> {'Archi', 'Idrish', 'Jay'}
set(['Idrish', 'Archi', 'Jay'])

I would think it would be consistent but it seems to not. Would love to know if any RP staff or others have any ideas. Anyways clearly we cannot assume the ordering of sets at all.

Bartosz Zaczyński RP Team on Aug. 4, 2020

Sets don’t make any promises about the ordering of their members. They use hashing, which itself is an implementation detail of the Python interpreter and depends on multiple factors.

To avoid hash collisions, which is a well-known security exploit, Python chooses a random seed for the hash function every time it starts. As a result, the same object will produce different and seemingly random hash values in separate interpreter sessions:

$ python
>>> hash('Archi')
-863196821531266440

$ python
>>> hash('Archi')
4042034065582253111

$ python
>>> hash('Archi')
7069460679375787308

These numbers determine the order of your set members. If you’re aware of the risks involved, you can disable this mechanism, for example, by setting an environment variable:

$ PYTHONHASHSEED=123 python
>>> hash('Archi')
1934745164520175339

$ PYTHONHASHSEED=123 python
>>> hash('Archi')
1934745164520175339

$ PYTHONHASHSEED=123 python
>>> hash('Archi')
1934745164520175339

Then, you should be able to get a predictable order of set members. However, if you need them to retain insertion order, you’re better off using a different collection such as OrderedSet:

$ pip install orderedset
$ python
>>> from orderedset import OrderedSet
>>> oset = OrderedSet([1, 2, 3])
OrderedSet([1, 2, 3])

kiran on Aug. 4, 2020

@Bartosz Zaczyński killing thanks.

kiran on Aug. 6, 2020

A set itself may be modified. can you give example for this?

Bartosz Zaczyński RP Team on Aug. 6, 2020

Sets are mutable in Python. You can add or remove elements from them:

>>> fruits = {'apple', 'banana', 'orange'}
>>> fruits.add('peach')
>>> fruits
{'apple', 'orange', 'banana', 'peach'}
>>> fruits.remove('banana')
>>> fruits
{'apple', 'orange', 'peach'}

Become a Member to join the conversation.