0

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?

2
Contributors
2
Replies
16
Views
4 Years
Discussion Span
Last Post by farmwife
1

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.