DevNet Associate (DEVASC) v1.0Software Development and DesignHard

A network automation script needs to process a large dataset of network device configurations, where each configuration is represented as a string. The script must efficiently identify and remove duplicate configuration lines while preserving the original order of the unique lines. Which Python data structure is most suitable for this task, balancing efficiency and order preservation?

  1. AList
  2. BDictionary
  3. CSet
  4. DTuple
Show answer & explanation

Correct answer: A. List

To remove duplicates while preserving order, a common approach is to iterate through a list and add items to a new list only if they haven't been seen before. A `set` can be used as an efficient way to track seen items, but the result must be built into a `list` to maintain order. Simply converting to a `set` then back to a `list` will lose the original order, as sets are inherently unordered.

Why the other options are wrong

  • B. A `dictionary` stores key-value pairs and does not directly support unique ordered elements in this context.
  • C. A `set` efficiently removes duplicates but does not preserve the original order of elements.
  • D. A `tuple` is an ordered, immutable sequence. While ordered, it doesn't provide built-in mechanisms for efficient duplicate removal and reconstruction while maintaining order.

Python List with Order-Preserving Deduplication

To remove duplicates from a Python list while preserving the original order, one common efficient technique involves iterating through the original list, adding each element to a new list only if it has not been encountered before (tracked efficiently using a set).

  • Direct conversion to `set` and back to `list` loses order because `set` is unordered.
  • The technique: create an empty result list and an empty `seen` set.
  • Iterate through the original list: if an item is not in `seen`, add it to both the result list and the `seen` set.
  • This approach leverages the fast lookup time of sets (`O(1)` on average) while maintaining list order.

Memory trick: List for order, Set for speed, combined for unique order.

More Software Development and Design questions