🔍 Code Extractor

function unique_zip

Maturity: 32

Returns a unique list of tuples created by zipping multiple iterables together, removing any duplicate tuples while preserving order.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1140 - 1144
Complexity:
simple

Purpose

This function combines multiple iterables using zip() and then filters out duplicate tuples, returning only unique combinations. It's useful when you need to pair elements from multiple sequences and ensure no duplicate pairings exist in the result. The function maintains the order of first occurrence for each unique tuple.

Source Code

def unique_zip(*args):
    """
    Returns a unique list of zipped values.
    """
    return list(unique_iterator(zip(*args)))

Parameters

Name Type Default Kind
*args - - var_positional

Parameter Details

*args: Variable number of iterable arguments (lists, tuples, arrays, etc.) to be zipped together. Each iterable should ideally have the same length, though zip() will truncate to the shortest iterable. Can accept any number of iterables (0 or more).

Return Value

Returns a list of unique tuples, where each tuple contains one element from each input iterable at the corresponding position. Duplicates are removed based on the entire tuple comparison. If no arguments are provided, returns an empty list. The order of unique tuples corresponds to their first appearance in the zipped sequence.

Dependencies

  • itertools

Required Imports

The function requires 'unique_iterator' to be available in the same module or imported

Usage Example

# Assuming unique_iterator is defined elsewhere in the module
# Example implementation of unique_iterator for demonstration:
def unique_iterator(seq):
    seen = set()
    for item in seq:
        if item not in seen:
            seen.add(item)
            yield item

# Using unique_zip
list1 = [1, 2, 3, 1, 2]
list2 = ['a', 'b', 'c', 'a', 'b']
list3 = [10, 20, 30, 10, 20]

result = unique_zip(list1, list2, list3)
print(result)
# Output: [(1, 'a', 10), (2, 'b', 20), (3, 'c', 30)]

# With two lists
result2 = unique_zip([1, 2, 2, 3], ['x', 'y', 'y', 'z'])
print(result2)
# Output: [(1, 'x'), (2, 'y'), (3, 'z')]

Best Practices

  • Ensure all input iterables have compatible lengths or be aware that zip() will truncate to the shortest iterable
  • The function depends on 'unique_iterator' being available in scope - verify this dependency exists
  • For large datasets, be aware that this creates a full list in memory rather than returning an iterator
  • The uniqueness check is based on the entire tuple, not individual elements
  • Elements within the tuples must be hashable for the unique_iterator to work with set-based deduplication
  • Consider using a generator version if memory efficiency is important for large datasets

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function lzip 65.2% similar

    A convenience wrapper around Python's built-in zip function that returns a list instead of an iterator.

    From: /tf/active/vicechatdev/patches/util.py
  • function unique_iterator 59.0% similar

    A generator function that yields unique elements from an input sequence in order of first appearance, filtering out duplicates.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_unique_keys 48.5% similar

    Extracts unique key values from an ndmapping object for specified dimensions, returning an iterator of unique tuples.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_unique_documents 46.5% similar

    Identifies and separates unique documents from duplicates in a list by comparing hash values of document text content.

    From: /tf/active/vicechatdev/chromadb-cleanup/src/utils/hash_utils.py
  • function cross_index 45.2% similar

    Efficiently indexes into a Cartesian product of iterables without materializing the full product, using a linear index to retrieve the corresponding tuple of values.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse