What is zip () in python?
❮ Built-in Functions Show
ExampleJoin two tuples together: a = ("John", "Charles", "Mike") x = zip(a, b) Try it Yourself » Definition and UsageThe If the passed iterators have different lengths, the iterator with the least items decides the length of the new iterator. Syntaxzip(iterator1, iterator2, iterator3 ...) Parameter Values
More ExamplesExampleIf one tuple contains more items, these items are ignored: a = ("John", "Charles", "Mike") x = zip(a, b) Try it Yourself » ❮ Built-in Functions The Example
Syntax of zip()The syntax of the zip(*iterables) zip() Parameters
Recommended Reading: Python Iterators, __iter__ and __next__ zip() Return ValueThe
Example 1: Python zip()
Output [] {(2, 'two'), (3, 'three'), (1, 'one')} Example 2: Different number of iterable elements
Output {(2, 'TWO'), (3, 'THREE'), (1, 'ONE')} {(2, 'two', 'TWO'), (1, 'one', 'ONE')} The * operator can be used in conjunction with zip(*zippedList) Example 3: Unzipping the Value Using zip()
Output [('x', 3), ('y', 4), ('z', 5)] c = ('x', 'y', 'z') v = (3, 4, 5) Python zip() method takes iterable or containers and returns a single iterator object, having mapped values from all the containers. It is used tomap the similar index of multiple containers so that they can be used just using a single entity.
Python zip() exampleExample 1: Python zip two listsPython3
Output: {('Shambhavi', 3), ('Nikhil', 1), ('Astha', 2), ('Manjeet', 4)} Example 2: Python zip enumeratePython3
Output: 0 Mukesh 24 1 Roni 50 2 Chari 18 Example 3: Python zip() DictionaryPython3
Output: {'reliance': 2175, 'infosys': 1127, 'tcs': 2750} How to unzip?Unzipping means converting the zipped values back to the individual self as they were. This is done with the help of “*” operator. Python3
Output: The zipped result is : [('Manjeet', 4, 40), ('Nikhil', 1, 50), ('Shambhavi', 3, 60), ('Astha', 2, 70)] The unzipped result: The name list is : ('Manjeet', 'Nikhil', 'Shambhavi', 'Astha') The roll_no list is : (4, 1, 3, 2) The marks list is : (40, 50, 60, 70) Practical ApplicationsThere are many possible applications that can be said to be executed using zip, be it student database or scorecard or any other utility that requires mapping of groups. A small example of scorecard is demonstrated below. Python3
Output: Player : Sachin Score : 100 Player : Sehwag Score : 15 Player : Gambhir Score : 17 Player : Dravid Score : 28 Player : Raina Score : 43 What is zip function in pandas?One of the way to create Pandas DataFrame is by using zip() function. You can use the lists to create lists of tuples and create a dictionary from it. Then, this dictionary can be used to construct a dataframe. zip() function creates the objects and that can be used to produce single item at a time.
What does zip do in NumPy?If we want to bind or zip the data of different array then we can go for zip function in python of NumPy. This function eliminates the need of class object if not required. We can easily map our data with any number of the array and this can be done very easily with the use of the zip() function.
What is difference between zip () and enumerate () Explain with example?The Python Tutorials Blog
The enumerate() function returns indexes of all items in iterables (lists, dictionaries, sets, etc.) whereas the zip() function is used to aggregate, or combine, multiple iterables.
|