Function to return True if integer k and n in list?
<p>Very new to python... If I give a list that has two integers n and k, the function should return Boolean True. K has to be in the list the number of n times. For example, if I call matchingValues([1, 2, 1, 4, 1, 1], 1, 4) should return True since 1 is in the list 4 times. I'm guessing I have to use the .count() option but not sure how to write it... Wish I could give more code but unfortunately I am very lost</p>

<pre><code>    def matchingValues(aList,n,k):
</code></pre>

<p>What you want to do is define a variable which counts how many times n is in the list</p>

<pre><code>found = 0
</code></pre>

<p>and then, iterate over your list with a loop</p>

<pre><code>for x in range(0, len(aList)):
    if aList[x] == n:
        found += 1
return(found == k)
</code></pre>

<p>This is, however, if you want to avoid using the <code>count</code> function:</p>

<pre><code>return(aList.count(n)==k)
</code></pre>

<p>Using the manual, hand-written loop will allow you to exit the loop as soon as you know there are <code>k</code> <code>n</code>s in the list, if you want the loop to work like that (however, I have not provided that code. I have only written exactly what <code>count</code> does)</p>

<p>If you want there to be AT LEAST <code>k</code> <code>n</code>s in <code>aList</code>, this would work for you</p>

<pre><code>found = 0
for x in range(0, len(aList)):
    if aList[x] == n:
        found += 1
        if found == k:
            return True
return False
</code></pre>

python list