How to create a unique list from multiple lists with duplicate values in Python

1 Answer

0 votes
# ------------------------------------------------------------
# Create a unique list from multiple lists containing duplicates
# ------------------------------------------------------------

# We define a function so the logic is reusable and clean.
def unique_from_lists(*lists):
    """
    Accepts any number of lists and returns a single list
    containing only unique values.

    The algorithm:
    - Use Python's built‑in 'set' type, which automatically
      removes duplicates and provides O(1) average lookup time.
    - Sets are ideal for deduplication because they are fast
      and implemented in C.
    - After merging all lists into a set, convert back to a list
      to preserve a standard Python sequence type.
    """

    # Create an empty set to accumulate unique values
    unique_values = set()

    # Iterate through all provided lists
    for lst in lists:
        # Update the set with the contents of each list
        # 'update' efficiently adds all elements from the list
        unique_values.update(lst)

    # Convert the set back to a list
    # Sorting is optional; here we sort for deterministic output
    return sorted(unique_values)


# ------------------------------------------------------------
# Usage
# ------------------------------------------------------------

list_a = [1, 2, 3, 3, 3, 4, 6, 7, 8]
list_b = [3, 4, 5, 3, 5, 3, 6]
list_c = [6, 7, 8, 1]

result = unique_from_lists(list_a, list_b, list_c)

print(result)



'''
run:

[1, 2, 3, 4, 5, 6, 7, 8]

'''

 



answered 1 day ago by avibootz
...