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>You could <code>zip</code> the a sliced list with itself one position ahead, with steps equal to <code>2</code> to get adjacent elements. Then feed that to <a href="https://docs.python.org/3/library/functions.html#all" rel="nofollow"><code>all</code></a> with the condition you need:</p>

<pre><code>def threePairs(l):
    return all(i == j for i,j in zip(l[::2], l[1::2])) 
</code></pre>

<p><a href="https://docs.python.org/3/library/functions.html#zip" rel="nofollow"><code>zip</code></a> simply takes an element from each of the iterables supplied and returns it as a tuple until one of the sequences is exhausted. </p>

<p>So, for example, if <code>l = [5, 6, 3, 2, 1, 4]</code> and <code>zip</code> is used with <code>zip(l[::2], l[1::2])</code> you'd have:</p>

<pre><code># l[::2] = [5, 3, 1]
# l[1::2] = [6, 2, 4]
print(list(zip(l[::2], l[1::2])))
[(5, 6), (3, 2), (1, 4)] 
</code></pre>

python list python-3.x pair