DevNet Associate (DEVASC) v1.0Software Development and DesignEasy
A network engineer is automating device configurations using Python. They need to store a collection of unique IP addresses without any specific order and quickly check for the presence of a particular IP. Which Python data structure is best suited for this task?
- AList
- BDictionary
- CTuple
- DSet
Show answer & explanationAnswer & explanation
Correct answer: D. Set
A Python set is an unordered collection of unique elements, making it ideal for storing unique IP addresses and providing very efficient membership testing (checking if an IP exists).
Why the other options are wrong
- A. Lists allow duplicates and maintain order, but membership testing is O(n), not efficient for large collections.
- B. Dictionaries store key-value pairs; while efficient for lookup by key, they are not designed for a simple collection of unique items.
- C. Tuples are immutable lists, also allowing duplicates and ordered, with O(n) membership testing.
Python Set
A Python set is an unordered collection of unique and immutable elements. It supports mathematical set operations like union, intersection, difference, and symmetric difference.
- Elements must be unique
- Unordered collection
- Mutable (can add/remove elements)
- Fast membership testing (O(1) average)
Memory trick: Sets ensure uniqueness, like a VIP list where everyone's distinct.