This error frustrates me when I want to iterate over the elements of a set, for example when trying to convert the set to a list. How do I get around it?

Recommended Answers

All 2 Replies

You do not get this error when you iterate over the elements of a set.

elements={1,2,3}
for each in elements:
    print each

You do not get this error when you convert a set to a list.

listelements=list(elements)

You do get this error, when you try to index a set:

print elements[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing

The elements in a set are not ordered per definition.

K thanks for clarifying that. :) Problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.