'Why is my answer to this count question not working?

We have a conveyor belt of items where each item is represented by a different number. We want to know, out of two items, which one shows up more on our belt. To solve this, we can use a function with three parameters. One parameter for the list of items, another for the first item we are comparing, and another for the second item. Here are the steps:

  1. Define the function to accept three parameters: the list, the first item, and the second item

  2. Count the number of times item1 shows up in our list

  3. Count the number of times item2 shows up in our list

  4. If item1 shows up more, return item1. Otherwise, return item2

I've since learned there is a much easier way to answer this using the count function than what I came up with. But I don't understand why my solution is not producing the desired result.

def more_frequent_item(lst,item1,item2):
  countFirst = 0
  countSecond = 0
  for i in lst:
    if lst[i] == item1:
      countFirst += 1
    elif lst[i] == item2:
      countSecond += 1
  if countFirst >= countSecond:
    return item1
  else:
    return item2
#Uncomment the line below when your function is done
print(more_frequent_item([2, 3, 3, 2, 3, 2, 3, 2, 3], 2, 3))

Expected output:

3

Actual output:

2


Solution 1:[1]

for i in lst iterates over the values in lst, not their indices. As such you'll want to compare the two items against i directly rather than lst[i].

for i in lst:
  if i == item1:
    countFirst += 1
  elif i == item2:
    countSecond += 1

Solution 2:[2]

You're iterating lst directly, which means you're getting the actual values, not indices into the list. But you then look them up as if they are indices in the list, which means you repeatedly test the values at index 2 and 3 in your list, not the actual 2s and 3 seen in the list.

Change:

  for i in lst:
    if lst[i] == item1:
      countFirst += 1
    elif lst[i] == item2:
      countSecond += 1

to:

  for item in lst:  # Avoiding name i, to avoid implying it's an index
    if item == item1:
      countFirst += 1
    elif item == item2:
      countSecond += 1

Also, note, you have an edge case failure. The description says only return item1 when there are more of it than item2, you return it when it's more or equal to item2's count. The test at the end should be:

if countFirst > countSecond:

to fix.

Solution 3:[3]

When you do for num in list:, num are the values in the list. So you wouldn't need to do list[num] to access that value, you would just use num directly:

def more_frequent_item(lst,item1,item2):
    countFirst = 0
    countSecond = 0
    for i in lst:
        if i == item1:
            countFirst += 1
        elif i == item2:
            countSecond += 1
    if countFirst >= countSecond:
        return item1
    else:
        return item2
#Uncomment the line below when your function is done
print(more_frequent_item([2, 3, 3, 2, 3, 2, 3, 2, 3], 2, 3))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Kugelman
Solution 2 ShadowRanger
Solution 3 Diego Cuadros