Module 1 · Python Foundations for Security Data · Lesson 4 of 9

Count failed logins with Counter

Group failures by (user, source IP) after normalising the input, and see why a total isn't a detector yet.

In this lesson · about 20 minutes

  • Use collections.Counter with tuple keys
  • Normalise values before comparing
  • Explain why total counts need a time window

collections.Counter is a dict built for counting. With a tuple key you can count combinations:

from collections import Counter
failures = Counter()
failures[("root", "203.0.113.45")] += 1
failures.most_common(3)

Normalise first. "failure", "FAILURE" and " failure " are the same outcome to a human, but not to ==. Strip spaces and lower-case before comparing.

A count is not a detector

Ten failures for root over a whole day could be ten typos spread across the day, or ten guesses in thirty seconds. A total can't tell them apart. The project in Module 2 adds a time window.

Exercise

Practice challenge 03: failures per account and source

Build a Counter called failures keyed by (user, source_ip). Count only rows whose outcome is failure after stripping and lower-casing. Skip rows with any blank field.

Check your understanding0 of 2 correct

Question 1

1.root has 10 failures today. Do 10 failures always mean an attack?

Question 2

2.Without lower-casing, how would dave's 'FAILURE' row be treated?

Quiz score: 0 of 2

Exercises solved: 0 of 1

Tip: answer every quiz question correctly first.

Noviq Academy is free and for learning. All data in this course is synthetic and uses documentation IP ranges. Never paste real authentication logs or credentials into online tools. Python runs in your own browser with Pyodide; nothing you type is sent to Noviq.