Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Post #9230
1.15K
Forwarded from Machine Learning with Python

reversed() in Python - what supports it and what doesn'tThe 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 iteratorlist(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}) → TypeErrorWhy? 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})))








