How to find the index of value in python list?

Python Programming question - strings and indexing?

  • How do I index a list of an unknown length starting at a certain point, lets say the 5th character (index 4) and splice the list starting from that index to the final character in the list? example question: Write an expression whose value is the str consisting of all the characters (starting with the sixth) of the str associated with s . or... Given that worst_offenders has been defined as a list with at least 6 elements, write a statement that defines lesser_offenders to be a new list that contains all the elements from index 5 of worst_offenders and beyond. Do not modify worst_offenders .

  • Answer:

    say, you want to get the substring from a'th to b'th s[a:b] or if you want from the beginning to b'th: s[:b] or from a'th to the end: s[a:] this is called slicing and it also works with any sequence (e.g. list, tuple). FYI, a[:] is a python idiom for creating a shallow copy of a list FYI, there is also the extended slicing, which adds a third argument explained here: http://www.python.org/doc/2.3.5/whatsnew/section-slices.html

Hayden at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.