Distinct labels in encounter order

In Listing 1, we have a Python function range_to_string that receives a rectangular range represented as a nested list of strings and returns a comma-separated string.

Listing 1: A Python function range_to_string that returns the unique values from a two-dimensional list as a comma-separated string. The join method concatenates the strings in unique_values with commas (Question 1).
def range_to_string(cell_range):
    seen = set()
    unique_values = []

    for row in cell_range:
        for value in row:
            if value not in seen:
                seen.add(value)
                unique_values.append(value)

    return ",".join(unique_values)
Table 1: Example range represented by [["A", "D"], ["1", "1"], ["C", "A"]].
A B
1 A D
2 1 1
3 C A

What is returned by range_to_string when it receives the nested list represented in Table 1?

  1. A,1,C,D,1,A
  2. A,1,C,D
  3. A,D,1,C
  4. A,D,1,1,C,A
  • range_to_string(cell_range) must return distinct labels in the order they are first encountered, one row at a time. For example, range_to_string([["X", "X"], ["Y", "X"]]) should return "X,Y".
  • set() creates an empty set. seen.add(value) adds a value once and returns None; adding "X" again does not create another member.
  • unique_values.append(value) adds one item to the end of the list and returns None.
  • ",".join(values) returns the strings joined by commas: ",".join(["X", "Y"]) is "X,Y", and joining [] returns "".