🔍 Code Extractor

function lzip

Maturity: 30

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

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1133 - 1137
Complexity:
simple

Purpose

This function provides a shorthand for list(zip(*args)), which is commonly needed when you want to immediately materialize the result of zipping multiple iterables into a list of tuples. It's particularly useful in data processing pipelines where you need the full list rather than a lazy iterator, or when you need to iterate over the zipped result multiple times.

Source Code

def lzip(*args):
    """
    zip function that returns a list.
    """
    return list(zip(*args))

Parameters

Name Type Default Kind
*args - - var_positional

Parameter Details

*args: Variable number of iterable arguments to be zipped together. Each argument should be an iterable (list, tuple, string, etc.). The function will pair up elements from each iterable at corresponding positions. If iterables have different lengths, the result will be truncated to the length of the shortest iterable.

Return Value

Returns a list of tuples where each tuple contains elements from the input iterables at the same index position. For example, lzip([1,2,3], ['a','b','c']) returns [(1,'a'), (2,'b'), (3,'c')]. If no arguments are provided, returns an empty list. If one argument is provided, returns a list of single-element tuples.

Usage Example

# Basic usage with two lists
result = lzip([1, 2, 3], ['a', 'b', 'c'])
print(result)  # [(1, 'a'), (2, 'b'), (3, 'c')]

# With three iterables
result = lzip([1, 2], ['a', 'b'], [True, False])
print(result)  # [(1, 'a', True), (2, 'b', False)]

# With different length iterables (truncates to shortest)
result = lzip([1, 2, 3], ['a', 'b'])
print(result)  # [(1, 'a'), (2, 'b')]

# Unpacking a list of lists
data = [[1, 2, 3], ['a', 'b', 'c']]
result = lzip(*data)
print(result)  # [(1, 'a'), (2, 'b'), (3, 'c')]

Best Practices

  • Use this function when you need the full list immediately rather than a lazy iterator, such as when you need to iterate multiple times or check the length
  • Be aware that with large iterables, this will consume memory to store the entire list, unlike the built-in zip which returns an iterator
  • Remember that the result is truncated to the length of the shortest input iterable
  • This is a pure utility function with no side effects, making it safe to use in any context
  • Consider using the built-in zip() directly if you only need to iterate once, as it's more memory efficient

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function unique_zip 65.2% similar

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

    From: /tf/active/vicechatdev/patches/util.py
  • function wrap_tuple 43.5% similar

    A utility function that ensures the input is wrapped in a tuple, leaving existing tuples unchanged and wrapping non-tuple values in a single-element tuple.

    From: /tf/active/vicechatdev/patches/util.py
  • function flatten 39.7% similar

    Recursively flattens an arbitrarily nested sequence containing lists, tuples, and dictionaries into a single-level generator of non-sequence elements.

    From: /tf/active/vicechatdev/patches/util.py
  • function cross_index 39.0% 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
  • function unpack_group 34.2% similar

    Unpacks a pandas DataFrame group by iterating over rows and yielding tuples of keys and objects, with special handling for objects with 'kdims' attribute.

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