Web2py comparing part of a request.vars element
<p>I have a form with a table with rows containing SELECTs with _names with IDs attached, like this:</p>

<pre><code>TD_list.append(TD(SELECT(lesson_reg_list, _name='lesson_reg_' + str(student[4]))))
</code></pre>

<p>When the form is submitted I want to extract both the student[4] value <em>and</em> the value held by request.vars.lesson_reg_student[4].</p>

<p>I've tried something like:</p>

<pre><code>for item in request.vars:
    if item[0:9] == "lesson_reg":
        enrolment_id = int(item[10:])
        code = request.vars.item
</code></pre>

<p>I also tried treating request.vars like a dictionary by using:</p>

<pre><code>for key, value in request.vars:
    if key[0:9] == "lesson_reg":
        enrolment_id = int(key[10:])
        code = value
</code></pre>

<p>but then I got 'too many values to unpack'. How do I retrieve the value of a request.vars item when the last part of its name could be any number, plus a substring of the item name itself?</p>

<p>Thanks in advance for helping me.</p>

<p>In Python, when slicing, the ending index is <em>excluded</em>, so your two slices should be <code>0:10</code> and <code>0:11</code>. To simplify, you can also use <code>.startswith</code> for the first one:</p>

<pre><code>for item in request.vars:
    if item.startswith('lesson_reg'):
        enrolment_id = int(item[11:])
        code = request.vars.item
</code></pre>

python variables request substring web2py