function unique_zip
Returns a unique list of tuples created by zipping multiple iterables together, removing any duplicate tuples while preserving order.
/tf/active/vicechatdev/patches/util.py
1140 - 1144
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function lzip 65.2% similar
-
function unique_iterator 59.0% similar
-
function get_unique_keys 48.5% similar
-
function get_unique_documents 46.5% similar
-
function cross_index 45.2% similar