Code Challenge: Lists More Frequent Item

https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-lists-cc/exercises/more-frequent

Can someone give a little insight. I am looking at this thinking logically different that it comes out. The def is asking for three things, now I am logically trying to look at this and get out of it more than what is there.

Item1 > Item2
Item2 > Item1
Item1 = Item2

With these I am looking to see how two are logic modifiers for a single item1 as the def asks for.

****** Question asked

Create a function named more_frequent_item that has three parameters named lst , item1 , and item2 .

Return either item1 or item2 depending on which item appears more often in lst .

If the two items appear the same number of times, return item1 .

#Write your function here
def more_frequent_item(lst, item1, item2):
  if lst.count(item2) >= lst.count(item1):
    return item2
  else:
    return item1
#Uncomment the line below when your function is done
print(more_frequent_item([2, 3, 3, 2, 3, 2, 3, 2, 3], 2, 3))

If they are equal technically neither is more, that is why I ask.

If this code works than yeah, you are correct. This code will always return list1 if the number is equal, even tho its technically wrong

Correct but again logically why would it return list1, it should return something along the lines of “There is a tie!”

Logically though that could be different, for instance if it list one being the lower is considered more relevant.

Your function satisfies the assignment, full-stop. In a sense, it is your job as a coder to provide what the customer (or instructor) requests.

But it usually doesn’t hurt to suggest changes where indicated if there is an avenue through which to do so, or even to implement them on your own as an exercise if there is not.

Remember, though, that this function might be part of a script that is expecting integer output, so returning a string could possibly lead to trouble.

Ok so here is my reasoning, this essentially the same question. My code satisfied the assignment but was still wrong and in this case backwards. That is why I asked of the logic involved in the process to develop this result.

Write a function named larger_list that has two parameters named lst1 and lst2 .

The function should return the last element of the list that contains more elements. If both lists are the same size, then return the last element of lst1 .

def larger_list(lst1, lst2):
  if len(lst2) > len(lst1):
    return lst2[-1:]
  else:
    return lst1[-1:]


#Uncomment the line below when your function is done
print(larger_list([4, 10, 2, 5], [-10, 2, 5, 10]))

This is wrong

def larger_list(lst1, lst2):
  if len(lst1) >= len(lst2):
    return lst1[-1]
  else:
    return lst2[-1]

#Uncomment the line below when your function is done
print(larger_list([4, 10, 2, 5], [-10, 2, 5, 10]))

Yet the answer is the same

In this case the colon made the difference as a display of the slice versus the display of the index number.

The slice version will return a list, not a number.

A slice (of a list) is a list, even of only one element. A list element identified by an index is a single element, of whatever type.

my_list = [1,2,3,4]
print(my_list[-1:])
print(my_list[-1])

# Output:
[4]
4
1 Like

Got it thanks :). Appreciate the feedback.