Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python .index() Function not Working in VS Code

Following a tutorial and this program won’t run as expected

my_tuple = {'a', 'p', 'p', 'l', 'e'}

print(my_tuple.index('p'))

Whenever I run this program, the terminal always outputs:

    print(my_typle.index('p'))
AttributeError: 'set' object has no attribute 'index'

Does VS Code’s Python not run these functions or, am I doing something wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s not a tuple, it’s a set. A tuple uses Parentheses and an set uses Curly Brackets.

https://www.w3schools.com/python/python_tuples.asp
https://www.w3schools.com/python/gloss_python_set.asp

Proper code:

my_tuple = ('a', 'p', 'p', 'l', 'e')

print(my_tuple.index('p'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading