Attention: Python Set is Changing From Time to Time

Hongtao Hao / 2022-08-12


I just learned this several days ago: set in python is unsorted and changes from time to time.

For example:

a = ['a', 'b', 'b', 'c', 'd', 'd', 'e']
set(a)

If you run the codes twice (in two different notebooks or scripts), the results are different. To solve this problem:

ls = list(set(a))
ls.sort()
ls
#tutorial

Last modified on 2022-10-30