Python List Segregation
<p>I have a list of roughly 30,000 items that I am currently printing into a table with their associated relationships. I want to break apart this list into alphabetically segregated pages. </p>

<p>The data comes from a database, and the list is formed with the following code:</p>

<p><code>ListView.as_view(queryset=TblCompanies.objects.all().order_by("company_name"),
                                    template_name="customers/list.html"))</code></p>

<p>Current list code is quite simple:</p>

<pre><code>{% for TblCompanies in object_list %}
    &lt;tr&gt;
        &lt;td width="35"&gt;{{ TblCompanies.company_id }}&lt;/td&gt;
        &lt;td width="10"&gt;   &lt;/td&gt;
        &lt;td&gt;{{ TblCompanies.company_name }}&lt;/td&gt;
        &lt;td&gt;{{ TblCompanies.phone_number }}&lt;/td&gt;
    &lt;/tr&gt;
{% endfor %}
</code></pre>

<p>Could someone advise me on how to go about doing this and break down the code for me?</p>

<pre><code>Name____#___#___#__              _____________________
acari | 1 | 2 | 3 |              |__A__|__B__|__C__|__  ...
angle | 1 | 2 | 3 |              |     |______________
arhat | 1 | 2 | 3 |              |Name_|_#_|_#_|_#_
akkra | 1 | 2 | 3 |              |
amiel | 1 | 2 | 3 |     TO       |acari| 1 | 2 | 3 |
barce | 1 | 2 | 3 |              |angle| 1 | 2 | 3 |
bogie | 1 | 2 | 3 |              |arhat| 1 | 2 | 3 |
betty | 1 | 2 | 3 |              |akkra| 1 | 2 | 3 |
brill | 1 | 2 | 3 |              |amiel| 1 | 2 | 3 |
blyth | 1 | 2 | 3 |              |
</code></pre>

<p>If the list is a text file, you can use pandas</p>

<pre><code>df = pd.read_csv(filepath,sep='|')
</code></pre>

<p>I'm going to use a dummy df to show querying</p>

<pre><code>df = pd.DataFrame(np.array(['alaska',1,2,'alabama',1,2,'brooklyn',1,2]).reshape(3,3))
ar2 = [df.values[n] if df.values[n][0].startswith('a') else [] for n in range(len(df.values))]
df3 = pd.Dataframe(np.array(ar2))
df3.to_csv('outputfile','|')
</code></pre>

python django