Sobes.tech
Junior — Middle

Can an element be extracted by its position from an unordered set?

sobes.tech AI

Answer from AI

A set in Python is an unordered collection of unique elements, so you cannot directly retrieve an element by position.

If you need to get an element by index, you can convert the set to a list, but this will create a copy and does not guarantee order:

s = {10, 20, 30}
item = list(s)[1]  # could be any element, order is not defined

Without sorting and converting the set, it is impossible to get an element by position, as the set does not store element order.