Shiva Krishna Bavandla
Shiva Krishna Bavandla

Reputation: 26728

Fetching only required string from the list

Suppose i had list as below

list_fields = ['Full-Time',
 'Options:',
 'Express your interest',
 'Email this to a friend',
 'More information about this job:',
 'Overview:',
 'A',
 'Parasite',
 'position is cool',
 'Are you a LEADER?',
 'For sure there',
 'Hello doing?',
 'If so you will EXCEL.',
 'Bring your skills',
 'programs and procedures']

what i am trying to do is collecting all the strings after overview, i mean need to ignore the strings before overview string. There may many strings before and after overview, but want to collect all the strings only after overview as a list and want to make them as a single string by using something like ''.join([list_fields]) Sorry if i am using words again and again, can anyone please tell me how to do this

Edited Code:
  1. Can we only fetch from after "overview:" to before 'Bring your skills'.

Thanks in advance

Upvotes: 3

Views: 120

Answers (4)

Vinayak Kolagi
Vinayak Kolagi

Reputation: 1881

Slice should work with index. This doesn't handle the error if not found.

lstResults = list_fields[list_fields.index('overview'):]

Upvotes: -1

zenpoy
zenpoy

Reputation: 20136

a = [1,2,3,4,5,6,7,8]
a[a.index(4):]

>> [4, 5, 6, 7, 8]

So in your case you can do:

"".join(list_fields[list_fields.index("Overview:"):])

To answer the questions in the comments:

def my_slice(l,a,b):
    try:
        a_idx = l.index(a)
        b_idx = l.index(b)

    except ValueError:
        return []

    if a_idx > b_idx:
        a_idx, b_idx = b_idx, a_idx
    return l[a_idx:b_idx+1]

my_slice(list_fields,'Overview:','Bring your skills')

Upvotes: 2

eumiro
eumiro

Reputation: 213015

import itertools as it

list(it.dropwhile(lambda x: not x.lower().startswith('overview'), list_fields))[1:]

Upvotes: 3

Sven Marnach
Sven Marnach

Reputation: 602285

If you know that the string is litterally "Overview:", you can do

''.join(list_fields[list_fields.index("Overview:") + 1:])

This uses list.index() to determine the index of "Overview:" in the list, and uses slicing to get the sublist starting at the index after "Overview:".

Upvotes: 5

Related Questions