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

Question regarding negative indices in tuples and lists in python

In this program,

t=(1,2,3,4,5,6)   
print(t[-1:-4])

Why is the output:

()

and not,

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

(6,5,4)

This is the case with lists also. I noticed print(t[-4:-1]) doesn’t give the output as () but instead gives (3, 4, 5)

What is going on here? why can’t we go backwards. Please help me I’ve just started python, I have no background in programming!

>Solution :

You have to use a negative step:

>>> t[-1:-4:-1]
(6, 5, 4)
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