8

I am working on a Windows 7 os in a Python (3.2.2) shell. Trying to learn the language I entered and had returned the following:

>>> cast = {
    'cleese',
    'Palin',
    'Jones',
    'Idle'
    }
>>> print (cast[1])
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    print (cast[1])
TypeError: 'set' object does not support indexing
>>> cast.append('Gilliam')
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    cast.append('Gilliam')
AttributeError: 'set' object has no attribute 'append'

==========================

It seems as if the problem is not in the coding, but with how the program was installed.

I have installed, un-installed and installed again, but the resutl is the same. I there something I need to do before Python's shell is ready to be used?

hans

1
  • Is what you've posted here the complete code? It looks like you're defining a dict and then trying to access it using list notation. You should be seeing a syntax error here. Try cast = ["cleese","Palin","Jones","Idle"]. Notice the '[' instead of '{' Commented Jan 19, 2012 at 2:06

3 Answers 3

20

Python seems to work fine. The point is that set doesn't support indexing or appending. Try using a list instead ([] instead of {}). In place of appending, set has add, but indexing is out.

And Python has useful help,

>>> help(set)

prints a lot of info about sets.

Sign up to request clarification or add additional context in comments.

Comments

4

It seems that you were trying to define a list. However, you used braces {} instead of brackets []. The interpreter treated it as a dictionary rather than a list, so indexing and append() didn't work here.

1 Comment

As stated in the old answer, the interpreter treats cast as a set, not a dictionary. There are no key/value pairs to form a dict.
3

Just to mention in here, set's' do not support indexing, as they are hash based, it is very similar to dictionaries which don't support indexing as well. You can only access a dict by it's key.

If you need indexing, you can convert your set as follows:

convertedToList = list(set(1,2,3))

1 Comment

I hate python. Of course any reasonable person wants to iterate over a set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.