A bug in python with tuple index

i posted this bug( to https://github.com/python/cpython/issues/106717 ) but i am not sure it is a bug or i am in confuse.( i am beginner in python). plz test it on idle:


except last and first, the python answer is 5 for others. but i can not see 5 ‘b’ in this tuple.
thanks for attention

In which case it is more reasonable to assume some misunderstanding, rather than finding a bug in a very simple aspect of the language :wink:

Tuples are 0-indexed (as is everything else in python). The first element is index 0. Index 5 in this tuple is a b


You have asked for the INDEX not the COUNT of ‘b’.

For your tuple these the indices:

t = ('s', 'b', 'c', 'a', 's', 'b')
for index in range(len(t)):
     print('Index', index, 'contains', t[index])

Which prints:

Index 0 contains s
Index 1 contains b
Index 2 contains c
Index 3 contains a
Index 4 contains s
Index 5 contains b

When you ask for the index of ‘b’ you will get 1 or 5 depending on where you start searching the tuple.

Also note that index starts at 0, not 1.

1 Like

I’m new to programming as well; MIT has their entire Comp. Sci. course online for free. Lecture 5 actually goes in depth into tuples. Great resource and if you’re interested, just look up MIT OCW 6.0001 and click on Lecture Videos.

@AlexWaygood I didn’t want to “help them understand what this method does”. I wanted them to clarify whether they actually got that wrong value (which would be an actual bug). But I see they left it out here, so apparently they didn’t.

I don’t know why you think that should have anything to do with the result. Let’s first check what you do understand.

Where the code says, for example,

What do you think this should mean? Step by step, how would you decide what the answer should be? What do you think the answer should be, and why? (Do you understand the word index? If it is not familiar, did you try to use an English dictionary to look it up?)

for given t:
t.index(‘b’,1) must be 1
t.index(‘b’,2) must be 5
t.index(‘b’.3) MUST give a message that the ‘b’ 's in t finished but python gives again 5. and so on.

hi. can give me the link of it?

Look at what i posted showing the index that corresponds with each character in your tuple.

What you asked for with t.index(‘b’, 3) is the position in t of the first ‘b’ starting at position 3.
And the answer is 5.

Ah, I think your mistake is that you think index(x, 3) means “give me the index of the third value of x”. That’s not what it means. As others have said already, it means “give me the index of the next value of x, starting from position 3”

The lecture I was referencing was Lecture 5: Tuples, Lists, Aliasing, Mutability, and Cloning | Introduction to Computer Science and Programming in Python | Electrical Engineering and Computer Science | MIT OpenCourseWare . This goes over Tuples in-depth.

Ah, I understand the reasoning now.

You expect that the 1 means “find the first instance”, the 2 means “find the second instance”, etc.

This is wrong.

The 1 means “start looking at element 1”, the 2 means “start looking at element 2”, etc.

Please remember that we start counting at zero, so “element 1” of the input ('s','b','c','a','s','b') is the first 'b', not the first 's'. We already found a 'b' since we started at one, so the result is 1. When we start looking at element 2, we start at the 'c', and keep looking until we find the second 'b', which is in position 5 (not 6, because we start counting at zero). If we start looking at element 3, that is the 'a'; we will look and find the second 'b' again, which is still in position 5. And so on.

1 Like

thanks for your explenation