By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,510 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,510 IT Pros & Developers. It's quick & easy.

List index question

P: n/a
I want to do list index function.
y=['1','2','3','4']
y ['1', '2', '3', '4'] y.index['2']

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsubscriptable object

It works with y=[1,2,3,4]. Anyone has any hint, what's the reason
here?

thanks

Dec 7 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
questions? wrote:
I want to do list index function.
y=['1','2','3','4']
y ['1', '2', '3', '4'] y.index['2'] Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsubscriptable object

It works with y=[1,2,3,4]. Anyone has any hint, what's the reason
here?


You're mixing things up badly. Either you want to call the function .index()
of the list object, in which case you don't use angle brackets but rather
round brackets (which works):
y = ['1','2','3','4']
y.index('2') 1

or you want to get an element at a specified position in the list, in which
case you don't use .index() but rather index the object using []:
y = ['1','2','3','4']
y[2] '3'

What you're trying to do is use a string as an index, which is bound to
fail:
y['2']

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list indices must be integers

And the error message says it all: a list index must be an integer.

I guess you better do some reading up on Python syntax... Have you done the
tutorial?

--- Heiko.
Dec 7 '05 #2

P: n/a
sorry, I realized the problem already. sorry for the confusion.
THanks for the answer!!!

Dec 7 '05 #3

P: n/a
"questions?" wrote:
I want to do list index function.
y=['1','2','3','4']
y ['1', '2', '3', '4'] y.index['2']
make that:
y.index('2')


</F>

Dec 7 '05 #4

P: n/a
What you mean to do is y.index('2'), rather than y.index['2']. Call
the index method of the list, rather than try to use index as if it was
itself a list.

Dec 7 '05 #5

P: n/a
What you mean to do is y.index('2'), rather than y.index['2']. Call
the index method of the list, rather than try to use index as if it was
itself a list.

Dec 7 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.