They differ because they are different objects with different contents.
Besides the bracket differences, a list is a sequence. When you put an element at the end of a list (via
The set doesn't maintain an order. The printed order depends on how many elements are in the set, and the hashed value of the object id()s within. If you need a particular order from a set, you have to establish it yourself. If you have to maintain a particular order, a set is the wrong collection to use.
Besides the bracket differences, a list is a sequence. When you put an element at the end of a list (via
append()
), it stays in that position. A fixed order is one of the properties of the list.The set doesn't maintain an order. The printed order depends on how many elements are in the set, and the hashed value of the object id()s within. If you need a particular order from a set, you have to establish it yourself. If you have to maintain a particular order, a set is the wrong collection to use.
print(sorted(dup_free_set))