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

In Python how do I slice a list based on an item in the list?

list1 = ['5', '3', '2', '1']
j = '3'

How do I extract from that list from j to the end of the list? In this example giving me:

['3', '2', '1']

when I know j, j is always in the list and the list is always filled with unique values?

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 :

so since you know the value of j here, we can get the index of j in the list using list1.index(j)
now we have to get all the items after j, so we can use this: list1[list1.index(j):]

what this basically does is, it gives all the values from the specified index (our case j) till the end of the list
this should give you the desired output!

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