On Fri, 29 Jan 2021 at 14:37, Francis O'Hara Aidoo <[email protected]>
wrote:

> I understand that the same effect can be achieved with the index notation
> - as in
> if listy[-1] == 10:
>     print("Monty Python")
> - but the way that came naturally to me was to use the .index method
> rather than index notation, and it took a very long time for me to figure
> out why my code was not working(mostly because I'm a beginner).
> So do what you will, I guess.:)
>
>
>
>
>
> Another issue here is that indexing and the list.index method are not
meant to be equivalent. In particular, the list.index method has to test
all the elements in your list before it finds the value you are looking
for, while indexing will just look up at the position you asked for, which
is much faster for long lists (especially since you are looking at the last
element in the list). In general, you shouldn't be using list.index if you
already know the index you are looking for.

Best,

E
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/NNMOHKXSG7N4AEHE4TYX33226BZYFOKY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to