Is There a Clear Winner?

Python for AIMedium+10 XP+10

Is There a Clear Winner?

Before training on labelled data, it's worth checking whether one label swamps all the others.

Task: write majority_label(labels) that returns the label that makes up more than half of the list. If no label does, return None.

  • Exactly half is not more than half: in a list of 4, a label needs at least 3.
  • An empty list returns None.
  • Labels are compared exactly as they are ("cat" and "Cat" are different).

Examples

labels = ["spam", "ham", "spam"]
returns 'spam'
labels = ["cat", "dog", "cat", "dog"]
returns None

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run