Pair 2 elements in a list and make a conditional statement to see if the pairs are equal to each other
<p>So I have this homework assignment and seem to be stuck on this question.</p>

<p>write a function that takes, as an argument, a list called aList. It returns a Boolean True if the list contains three pairs of integers, and False otherwise.</p>

<p>Example:</p>

<pre><code>&gt;&gt;&gt;threePairs([5, 6, 3, 2, 1, 4])
False

&gt;&gt;&gt;threePairs([1, 1, 2, 2, 2, 2])
True
</code></pre>

<p>I've tried using indexes and I don't really know how to slice so I'm stuck in figuring out how I make those pairs equal to each other in the condition so it is True.</p>

<p>This is what I had previous to deleting it and trying again.</p>

<pre><code>def threePairs(aList):
    if [0] == [1] and [2] == [3] and [4] == [5]:
        return True
    else:
        return False
</code></pre>

<p>How about making a <a href="https://docs.python.org/2/library/collections.html#collections.Counter" rel="nofollow"><code>Counter()</code></a> and check how many "pairs" you've got:</p>

<pre><code>In [1]: from collections import Counter

In [2]: def has_pairs(l, n):
            return sum(value / 2 for value in Counter(l).values() 
                       if value % 2 == 0) == n 

In [3]: has_pairs([5, 6, 3, 2, 1, 4], 3)
Out[3]: False

In [4]: has_pairs([1, 1, 2, 2, 2, 2], 3)
Out[4]: True
</code></pre>

<p>Works for a list with any length and any input number of pairs.</p>

<p>Instead of using a <code>sum()</code> to count all the pairs, you can iterate over the counter one value at a time and have an <em>early exit</em> if number of pairs reaches or exceeds the input number of pairs.</p>

python list python-3.x pair