title: How is it possible to extract sub-strings using keywords and index?
body: <p>I'm trying to get certain sub-string following a keyword from a data string. 
  These colleceted sub-strings are then joined together.</p>
  
  <p>Is there a simpler approach than multiple for-loops-iterations, without using regular expressions?</p>
  
  <p>I've tried to split the data string (-> list of words) and then iterate over it to temporarily save the index (in the list of words) where a keyword appeared.</p>
  
  <p>Using these indexes, I can tell that the sub-string of keyword A is between the index of keyword A and keyword B. So I join these strings from the list.</p>
  
  <p>This approach seems quite overcomplicated.</p>
  
  <pre class="lang-py prettyprint-override"><code>data_string = "safpifucr keywordA: ejeca pfiktecr 3697406324 keywordB: 2505098781 epef ahemahkuj  keywordC: ezivwut 2564556750 inanrune"
  keywords = ['keywordA:', 'keywordB:', 'keywordC:']
  split_string = data_string.split()
  keyword_index = []
  # Getting the indexes of keywords
  for index, word in enumerate(split_string, start=0):
      if word in keywords:
          keyword_index.append((word, index))
  
  
  def getSubString(split_string, key_index, next_key_index):
      subString = []
      for index, sub in enumerate(split_string, start=0):
          if index &gt; key_index and index &lt; next_key_index:
              subString.append(sub)
      return subString
  # Get SubString after Keyword A
  subA = getSubString(split_string, keyword_index[0][1], keyword_index[1][1])
  print(' '.join(subA)) # ejeca pfiktecr 3697406324
  
  # Get SubString after Keyword A
  subB = getSubString(split_string, keyword_index[1][1], keyword_index[2][1])
  print(' '.join(subB)) # 2505098781 epef ahemahkuj
  
  # Get SubString after Keyword A
  subC = getSubString(split_string, keyword_index[2][1], len(split_string))
  print(' '.join(subC)) # ezivwut 2564556750 inanrune
  </code></pre>
  
  <p>I was wondering if there was a simpler way to achieve the above result without having to iterate over the split_string multiple times. (n*keywords)</p>
  
  <h2>Update</h2>
  
  <p>I require this for looking through banking records that have all the <em>Transaction Info</em> in one continous String. The various relevant informations are all behind keywords followed by a colon.
  For various reasons I will not share the exact String but I've rebuilt some samples that are identical in structure just with fictional content.</p>
  
  <pre class="lang-py prettyprint-override"><code># Approach 2 of blhsing with RANDOM-DATA
  keywords = set(['Empfänger:', 'Verwendungszweck:', 'Zahlungsempfänger:', 'Auftraggeberinformation:', 'Zahlungsreferenz:'])
  mapping = {}
  keyword = None
  data_list = (
      'ONLINE BANKING VOM 1.10 UM 20:18  Empfänger:  Henrietta Mullins  Verwendungszweck:  bestellung 57E639 2019-06-10 terriblesuccess.SG',
      'SEPA Lastschrift  Zahlungsempfänger:  planetwötrap-AG  Verwendungszweck:  planetwötrap-AG-AG 460/487835-389 Ina Gill  Auftraggeberinformation:  3740930917-909/387187-3',
      'SEPA Lastschrift  Zahlungsempfänger:  AMAZON PAYMENTS EUROPE S.C.A.  Verwendungszweck:  409-7235170-354760 AMZN Mktp FI 3882385979  Auftraggeberinformation:  6V0RRQCT6GAACLH8',
      'POS 50,0648 FK K2 5.22 11:17 these cutting 5359')
  for data_string in data_list:
      for word in data_string.split():
          if word in keywords:
              keyword = word
              print('if word in keywords: ', word)
          elif keyword in mapping:
              mapping[keyword] += ' ' + word
              print('elif keyword in mapping: ', word)
          elif keyword:
              mapping[keyword] = word
              print('elif keyword: ', word)
  print(mapping)
  </code></pre>
  
  <p>This outputs</p>
  
  <p><code>{'Empfänger:': 'Henrietta Mullins', 'Verwendungszweck:': 'bestellung 57E639 2019-06-10 terriblesuccess.SG SEPA Lastschrift planetwötrap-AG-AG 460/487835-389 Ina Gill 409-7235170-354760 AMZN Mktp FI 3882385979', 'Zahlungsempfänger:': 'planetwötrap-AG AMAZON PAYMENTS EUROPE S.C.A.', 'Auftraggeberinformation:': '3740930917-909/387187-3 SEPA Lastschrift 6V0RRQCT6GAACLH8 POS 50,0648 FK K2 5.22 11:17 these cutting 5359'}</code></p>