Python zip() FunctionPython zip() function returns a zip object, which maps a similar index of multiple containers. It takes iterables (can be zero or more), makes it an iterator that aggregates the elements based on iterables passed, and returns an iterator of tuples. SignatureParametersiterator1, iterator2, iterator3: These are iterator objects that joined together. ReturnIt returns an iterator from two or more iterators. Python zip() Function Example 1The below example shows the working of zip(). Output: [] {(5, 'five'), (4, 'four'), (6, 'six')} Explanation: In the above example, we have initialized the lists, i.e., numList (contains integer values) and strList (contains string values) and map the values by using zip(). In the first case, it converts iterator to a list and prints resultList that contains no values. Then, two iterables are passed in the zip(), i.e., numList, strList and convert iterator to set and return its values. Python zip() Function Example 2The below example unzip the values using zip(). Output: [('x', 6), ('y', 2), ('z', 8)] c = ('x', 'y', 'z') v = (6, 2, 8)
Next TopicPython Built in Functions
|