Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

set() in Python

I have used the set() function but I am confused.

x = set("car")
print(x)

Why does this code output: "a", "c", "r" and not "car"? Thanks for your answers.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s because of the main properties of set in Python.

  • They can’t contain duplicates
  • They’re unordered

So things like this one just happen.

>>> set("car")
{'a', 'r', 'c'}

I suggest you to read this useful documentation about Python’s set.


If you really want an ordered set, there’s something that can be useful to you.

I would suggest you to check this answers too.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading