Explanation
- A **Zipper** is a data structure used to efficiently navigate and manipulate sequences. It’s often used in functional programming to manage pairs of sequences, typically by "zipping" together two lists.
-
Steps
- Zip two sequences into a sequence of pairs.
- Provide efficient operations for traversing and modifying these pairs.
Time Complexity
- Time complexity: O(n) for zipping two sequences of size
n.
def zip_lists(list1, list2):
return list(zip(list1, list2))
# Example usage
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
zipped = zip_lists(list1, list2)
print(zipped) # Outputs: [(1, 'a'), (2, 'b'), (3, 'c')]