Hardik Patel
Hardik Patel

Reputation: 155

Different ValueError for the same operation in List and Tuple

I am curious why ValueErrors are different in List and Tuple when I try to get an index. ValueError of a list returns in well format with actual argument "ValueError: 'ITEM' is not in list", whereas tuple returns something like this "ValueError: tuple.index(x): x not in tuple". I think List and Tuple both are calling same index() method then why it is raising different ValueErrors?


>>> jframe_li
['Angular', 'React', 'Vue.js', 'Ember.js', 'Mereor', 'Node.js', 'Backbone.js']
>>> jframe_tu
('Angular', 'React', 'Vue.js', 'Ember.js', 'Mereor', 'Node.js', 'Backbone.js')
>>> jframe_li.index('React')
1
>>> jframe_tu.index('React')
1
>>> jframe_li.index('react')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'react' is not in list

>>> jframe_tu.index('react')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: tuple.index(x): x not in tuple

Upvotes: 2

Views: 704

Answers (1)

benvc
benvc

Reputation: 15120

There are implementation differences in the index methods for lists and tuples, including in the text of a raised ValueError.

See ValueError string for tuple.index and ValueError string for list.index

Upvotes: 4

Related Questions