Module 1 · Python Foundations for Security Data · Lesson 1 of 9
Python for defenders: lists, dicts, sets and tuples
The four data structures behind almost every security script, practised on IP addresses and login events.
In this lesson · about 22 minutes
- Choose between a list, dict, set and tuple
- Count and de-duplicate values
- Write a small function and run it in the browser
Security work produces piles of text: logs, alerts, exports. Python turns those piles into answers ("which accounts were attacked, from where, and when?"). You don't need much of the language to start. You need the right data structure:
| Structure | Looks like | Use it when |
|---|---|---|
| list | ["10.0.0.5", "10.0.0.7"] | Order matters, duplicates allowed: events in sequence |
| dict | {"root": 10, "alice": 1} | Look things up by key: counts per user |
| set | {"10.0.0.5", "10.0.0.7"} | Unique members only, fast "is it in there?": distinct IPs |
| tuple | ("root", "203.0.113.45") | A fixed group of values that can't change; can be a dict key |
Loops and functions
for item in items: repeats a block for each item. def parse_row(row): defines a reusable function that returns a value. Keep input parsing separate from detection logic: it makes both easier to test.
Every code box on this page really runs Python in your browser. Press Run (or Ctrl/Cmd + Enter). The first run takes a few seconds while Python loads.
Try it: four structures, one set of IPs
Run this, then change one value and run it again. Predict the result first.
Exercise
How many distinct sources?
The list ips holds the source address of every alert today. Set unique_count to the number of distinct addresses.
Code that runs before yours
ips = ["203.0.113.45", "198.51.100.7", "203.0.113.45", "192.0.2.15", "10.0.0.5", "192.0.2.15", "203.0.113.45"]
Exercise
Count alerts per address
Write a function count_by_ip(ips) that returns a dict mapping each address to how many times it appears.
Code that runs before yours
ips = ["203.0.113.45", "198.51.100.7", "203.0.113.45", "192.0.2.15", "10.0.0.5", "192.0.2.15", "203.0.113.45"]
Check your understanding0 of 3 correct
Quiz score: 0 of 3
Exercises solved: 0 of 2
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.