Categorize list in Python
<p>What is the best way to categorize a list in python?</p>

<p>for example:</p>

<pre><code>totalist is below

totalist[1] = ['A','B','C','D','E']
totalist[2] = ['A','B','X','Y','Z']
totalist[3] = ['A','F','T','U','V']
totalist[4] = ['A','F','M','N','O']
</code></pre>

<p>Say I want to get the list where the first two items are <code>['A','B']</code>, basically <code>list[1]</code> and <code>list[2]</code>.  Is there an easy way to get these without iterate one item at a time?  Like something like this?</p>

<pre><code>if ['A','B'] in totalist
</code></pre>

<p>I know that doesn't work.</p>

<p>You could check the first two elements of each list.</p>

<pre><code>for totalist in all_lists:
    if totalist[:2] == ['A', 'B']:
        # Do something.
</code></pre>

<p><strong>Note:</strong> The one-liner solutions suggested by Kasramvd are quite nice too. I found my solution more readable. Though I should say comprehensions are slightly faster than regular for loops. (Which I tested myself.)</p>

python list