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

Getting "TypeError:list indices must be integers"

P: n/a
Hi,
Please check out the following loop,here indexList1 and indexList2 are a
list of numbers.

for index1 in indexList1:
for index2 in indexList2:
if ti1[index1] == ti2[index2] and not index1 != indexList1.pop():
index1+=1
index2+=1
continue
elif index1 == indexList1.pop() and charList in pairList:
k = string2.index(char2[0])
instance = ti2(k)
tiNew = ti1.append(instance)
tiNewList.append(tiNew)
else:
break

On running my program, python gives me a TypeError saying:

if ti1[index1] == ti2[index2] and not index1 != indexList1.pop():
TypeError: list indices must be integers

Even though index1 and index2 are really integers.Please help!

Jun 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 13/06/2006 4:11 PM, Girish Sahani wrote:
Hi,
Please check out the following loop,here indexList1 and indexList2 are a
list of numbers.
Later you say they are integers. Note: Python calls 2.0 a float, not an
integer.
|>>> foo = [9, 8, 7]
|>>> foo[2.0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list indices must be integers
|>>>

for index1 in indexList1:
for index2 in indexList2:
if ti1[index1] == ti2[index2] and not index1 != indexList1.pop():
Wouldn't
... and index1 == indexList1.pop()
be easier to follow?

Didn't I read earlier today somebody being admonished for modifying a
list over which they were iterating? Who was that? You! What do you
think indexList1.pop() is doing???
index1+=1
index2+=1
The above 2 statements have no effect at all. In particular, they
*don't* affect the next value used by the relevant for statement, if
that's what you were hoping for.
continue
elif index1 == indexList1.pop() and charList in pairList:
k = string2.index(char2[0])
instance = ti2(k)
tiNew = ti1.append(instance)
tiNewList.append(tiNew)
else:
break

On running my program, python gives me a TypeError saying:

if ti1[index1] == ti2[index2] and not index1 != indexList1.pop():
TypeError: list indices must be integers

Even though index1 and index2 are really integers.Please help!


How do you know that they are integers? Have you followed the advice
given by readers of previous episodes of your adventures, and inserted
print statements at relevant points?

In this case, inserting
print repr(index1), repr(index2)
immediately after the 2nd for statement would be a very good idea.

Please do come back and tell us what you find out, *and* what you have
learned.

HTH,
John
Jun 13 '06 #2

P: n/a
On 13/06/2006 4:11 PM, Girish Sahani wrote:
[snip]
instance = ti2(k)
tiNew = ti1.append(instance)


ti2 is quacking "function" but ti1 is quacking "list".

Possibilities:
(1) You meant to type ti2[k] ... and this section of code has not yet
been executed, and would have featured as episode N+1 had morbid
curiosity not led me to read further.
(2) You have the weirdest system of choosing names that I have seen for
decades.
(3) Both of the above.

Cheers,
John
Jun 13 '06 #3

P: n/a
Hi ppl,
I'm really sorry for the previous post. I write mails very quickly and end
up making errors in it.
This time i ended up giving a code portion from an old copy of my program.
Here's the code portion that is giving a TypeError:list indices must be
integers

for index1 in indexList1:
for index2 in indexList2:
if ti1[index1] == ti2[index2] and index1 != indexList1[-1]:
index1+=1
index2+=1
continue
elif index1 == indexList1[-1] and charList in pairList:
k = string2.index(char2[0])
instance = ti2(k)
tiNew = ti1.append(instance)
tiNewList.append(tiNew)
else:
break
Jun 13 '06 #4

P: n/a
On 13/06/2006 5:08 PM, John Machin wrote:
On 13/06/2006 4:11 PM, Girish Sahani wrote:
[snip]
instance = ti2(k)
tiNew = ti1.append(instance)


ti2 is quacking "function" but ti1 is quacking "list".

Possibilities:
(1) You meant to type ti2[k] ... and this section of code has not yet
been executed, and would have featured as episode N+1 had morbid
curiosity not led me to read further.
(2) You have the weirdest system of choosing names that I have seen for
decades.
(3) Both of the above.


Episode N+2:

tiNew = ti1.append(instance)
doesn't do what you think it does:
foo = [9, 8, 7]
bar = 'xyz'
bar = foo.append(42)
foo [9, 8, 7, 42] repr(bar) 'None'


Cheers,
John
Jun 13 '06 #5

P: n/a
> On 13/06/2006 4:11 PM, Girish Sahani wrote:
[snip]
instance = ti2(k)
tiNew = ti1.append(instance)
ti2 is quacking "function" but ti1 is quacking "list".

Possibilities:
(1) You meant to type ti2[k] ... and this section of code has not yet
been executed, and would have featured as episode N+1 had morbid
curiosity not led me to read further.

That is corrected. I'm appending a particular element of ti2 to ti1.
It hasnt been executed because i'm stuck on that TypeError since 2 hours
:( (2) You have the weirdest system of choosing names that I have seen for decades. :(( (3) Both of the above.

Cheers,
John


Jun 13 '06 #6

P: n/a
On 13/06/2006 5:33 PM, Girish Sahani wrote:
Python prints 'c','c' when i print repr(index1),repr(index2).This means
they are characters right??
But i have defined indexList as follows (and also tested the output, both
are outputted as lists of numbers):

for char in list1:
i = substring1.index(c)
That would be "char", I presume, not "c".
indexList1.append(i)

for char in list2:
j = substring2.index(char)
indexList2.append(j)

(substring1 and substring2 are 2 different strings)

Then i'm iterating over indexList1 and indexList2, so i fail to understand
why i am getting the typeError....

On 13/06/2006 5:08 PM, John Machin wrote:
On 13/06/2006 4:11 PM, Girish Sahani wrote:


[SNIPPED]

Girish,

Could we please abide by the Geneva Convention:

1. Please don't top-post.
2. Please don't type what you thought was in your code; copy/paste
actual most-recently-executed code.
3. Please reply to the newsgroup/mailing-list -- I've taken the liberty
of dragging this back there as there appears to be no private content ...

OK, so you've found that index1 and index2 each contain 'c'. Despite
your belief that they should contain the results of
some_string.index(some_char), the only reasonable hypothesis is that
somebody is polluting the water further upstream. Who is that somebody?
The usual and only suspect is *you*. Please go away and sprinkle print
statements at likely spots further upstream until you have found the
problem.

Kindest possible regards,
John
Jun 13 '06 #7

P: n/a
Girish Sahani wrote:
On 13/06/2006 4:11 PM, Girish Sahani wrote:
[snip]
instance = ti2(k)
tiNew = ti1.append(instance)

ti2 is quacking "function" but ti1 is quacking "list".

Possibilities:
(1) You meant to type ti2[k] ... and this section of code has not yet
been executed, and would have featured as episode N+1 had morbid
curiosity not led me to read further.

That is corrected. I'm appending a particular element of ti2 to ti1.
It hasnt been executed because i'm stuck on that TypeError since 2 hours
:( (2) You have the weirdest system of choosing names that I have seen for
decades.

:((
(3) Both of the above.

Cheers,
John

How about just inserting some print statements that show you the
type and value of each index before you use it. It is old fashioned,
but it actually works.

-Larry Bates
Jun 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.