简体   繁体   中英

Order of elements returned by set

So I was converting a list with repetitive elements into a set to have each element appear once. I know that sets are unordered so they will display the elements in the order given. I ran the below script and I noticed a strange output.

mylist = [1,2,2,33,4,4,11,22,3,3,2]

print(set(mylist))

The output would be:

{1, 2, 33, 4, 3, 11, 22}

The 3 in the original list appears after the 11 and 22, so why does it appear before them in the set?

Sets in Python do not have an order

A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM