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

A developer is designing a Python application that needs to manage network device configurations. The application frequently needs to store and retrieve unique identifiers for devices, along with their corresponding configuration settings. It's crucial that duplicate identifiers are not stored, and the order of insertion does not matter for the identifiers themselves, although the associated configuration might be complex. Which Python data structure is most suitable for efficiently handling the unique device identifiers?

  1. ASet
  2. BTuple
  3. CDictionary
  4. DList
Show answer & explanation

Correct answer: A. Set

A Python set is an unordered collection of unique elements. This directly addresses the requirement of storing unique device identifiers and ensures that duplicate identifiers are not stored. While a dictionary could store key-value pairs, the question specifically asks for the best structure for *unique identifiers* themselves, not the identifier-configuration mapping.

Why the other options are wrong

  • B. A tuple is an ordered, immutable collection that allows duplicate elements, making it unsuitable for storing unique identifiers.
  • C. A dictionary stores key-value pairs. While keys in a dictionary are unique, the question focuses on the data structure for the *identifiers* themselves where uniqueness is the primary concern, making a set a more direct fit for just the identifiers.
  • D. A list is an ordered collection that allows duplicate elements, which violates the 'unique identifiers' requirement.

Python Set

A Python set is an unordered collection of unique, immutable elements. It is highly efficient for membership testing and eliminating duplicates.

  • Elements must be hashable (immutable).
  • Does not maintain insertion order.
  • Supports mathematical set operations (union, intersection, difference).

Memory trick: Python Structures: Lists, Tuples, Sets, Dictionaries - pick the right tool for the job!

More Software Development and Design questions