473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

List index question

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
5 1471
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
sorry, I realized the problem already. sorry for the confusion.
THanks for the answer!!!

Dec 7 '05 #3
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike | last post by:
How do I extract a list of lists from a user defined function and print the results as strings for each list?
9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
9
by: danny van elsen | last post by:
hello all, I have an application in which I build a list<node>, with potentially thousands of nodes. each node has an "index", and all nodes are ordered by this index. this index reflects a...
1
by: Ian Davies | last post by:
Hello In a php file I have a drop down list with index numbers in whos default value is feed into an sql query that filters records from my database and displays them in an html table. Trouble...
6
by: Michael McGarry | last post by:
Hi, Please excuse me if this is not the right forum for this question. I would like to create a random permutation of a list of numbers. How can I do this in C? I was just going to draw a...
4
by: dustin.getz | last post by:
consider the following working loop where Packet is a subclass of list, with Packet.insert(index, iterable) inserting each item in iterable into Packet at consecutive indexes starting at index. ...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
3
by: Riccardo Murri | last post by:
Hello, I have some code that stops when trying to find a graph in a list of similar graphs:: (Pydb) list 110 try: 111 canonical = self.base 112 except ValueError: 113 ...
5
by: Tony | last post by:
Hello! Here I have a collection class Cards which is derived from the Base class CollectionBase. This class Cards is a container for Card object. Now to my question at the bottom of this class...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.