title: Parsing a nested form array in Flask, with support for using getlist() on elements that have several values
body: <p>Similar to <a href="https://stackoverflow.com/a/49819417/12718345">this answer</a>, I have a nested array structure being submitted by a form to Flask.</p>
  
  <p>However, my values <strong>can</strong> sometimes come from a multiselect dropdown (several values) and othertimes not, so I may need to parse them one step further with <a href="https://tedboy.github.io/flask/generated/generated/werkzeug.MultiDict.getlist.html" rel="nofollow noreferrer">getlist</a> but not always.</p>
  
  <p>Here is an example of <code>request.form</code>:</p>
  
  <pre><code>ImmutableMultiDict([('name', 'sadfsdaf'), ('description', ''), ('step_action[0]', 'forward'), ('step_values[0]', 'North America'), ('step_values[0]', 'South America')])
  </code></pre>
  
  <p>Notice how there are two values for <code>step_values[0]</code>.</p>
  
  <p>Normally, you would access these elements with:</p>
  
  <p><code>request.form.getlist('step_values[0]')</code></p>
  
  <p>I found a <a href="http://%20https://stackoverflow.com/a/49819417/12718345" rel="nofollow noreferrer">helper method</a> to parse the POSTed data into a nested array structure, but it doesn't support calling <code>getlist</code> if there are multiple values.</p>
  
  <p>Helper Method:</p>
  
  <pre><code>def parse_multi_form(form):
      """Returns a dictionary containing complex (multi-dimensional) form data."""
      """https://stackoverflow.com/a/49819417/12718345"""
      data = {}
      for url_k in form:
          v = form[url_k]
          ks = []
          while url_k:
              if '[' in url_k:
                  k, r = url_k.split('[', 1)
                  ks.append(k)
                  if r[0] == ']':
                      ks.append('')
                  url_k = r.replace(']', '', 1)
              else:
                  ks.append(url_k)
                  break
          sub_data = data
          for i, k in enumerate(ks):
              if k.isdigit():
                  k = int(k)
              if i + 1 &lt; len(ks):
                  if not isinstance(sub_data, dict):
                      break
                  if k in sub_data:
                      sub_data = sub_data[k]
                  else:
                      sub_data[k] = {}
                      sub_data = sub_data[k]
              else:
                  if isinstance(sub_data, dict):
                      sub_data[k] = v
  
      return data
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>{
    'name': 'sadfsdaf',
    'description': '',
    'step_action': {
      0: 'forward'
    },
    'step_values': {
      0: 'North America'
    }
  }
  </code></pre>
  
  <p>How can I modify the output to support multiple values under a single key, like this?</p>
  
  <pre><code>{
    'name': 'sadfsdaf',
    'description': '',
    'step_action': {
      0: 'forward'
    },
    'step_values': {
      0: ['North America', 'South America']
    }
  }
  </code></pre>
  
  <p>Note the addition of <code>South America</code> under <code>'step_values'[0]</code> above.</p>