function lzip
A convenience wrapper around Python's built-in zip function that returns a list instead of an iterator.
/tf/active/vicechatdev/patches/util.py
1133 - 1137
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
-
function wrap_tuple 43.5% similar
-
function flatten 39.7% similar
-
function cross_index 39.0% similar
-
function unpack_group 34.2% similar