Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Immutable Data Types

Why {False, True}?

Hi everyone!

I'd like to know why {True, False} gives {False, True} when printed every time, without changing order. Also, why {False, True} instead of {True, False}?

I've noticed that if I print {None, True, False} it then gives {None, True, False} instead of {None, False, True}. What is this madness? Thank you for your help.

2 Answers

Steven Parker
Steven Parker
231,007 Points

In Python, a set is an unordered collection, which means that the order of elements in a set is undefined. While sets are iterable, they do not support indexing, slicing, sorting, or other sequence-like behavior. The order that you see when listing the entire set is simply a result of the optimization algorithms in the engine implementation.

A Python engine on a different platform, or a different version, might list the same set in a different order.

Thanks!