TGViewer
Learn Python Coding Learn Python Coding @pythonre · 40.8K subscribers
Post #9230 1.15K

Forwarded from Machine Learning with Python

reversed() in Python - what supports it and what doesn't

The function reversed() is built-in in Python, but it doesn't work with all data types

✓ Lists - it works
reversed([1, 2, 3]) returns an iterator
list(reversed([1, 2, 3])) → [3, 2, 1]

✓ Tuples - it also works
reversed((1, 2, 3)) can be easily iterated

✗ Sets - not supported
reversed({1, 2, 3}) → TypeError
Why? Sets don't have a fixed order, so they can't be "reversed"

If you need to reverse a set:
list(reversed(list({1, 2, 3})))
More from @pythonre
  1. Oct 10, 2026📚 Download 10 Python books for FREE: 1. Think Python — O’Reilly https://t.co/X7y3pX68IW 2…
  2. Oct 7, 2026Python Lists: A Quick Reference 🐍 * Definition: Lists are ordered, mutable collections th…
  3. Oct 7, 2026📱 270 tools for Python developers The repository contains: 🫡 Linters and formatters; 🫡…
  4. Oct 7, 2026Hands On Python Data Science - Data Science Bootcamp Master Python for Data Science with R…
  5. Oct 6, 2026Post #9851
  6. Oct 6, 2026Python For Beginners Course In-Depth Python: A Comprehensive Introduction to Fundamental P…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →