Python zip() function Example
Hello in this tutorial, we will explain the zip()
function in the python programming language.
1. Introduction
The zip()
function in python takes multiple iterables and aggregates them into a python tuple (i.e. collection of objects) and returns it. The syntax of zip()
function in python programming is –
Syntax
1 2 | # iterables are the built-in iterables (like list, string, dict) or the user-defined variables zip ( * iterables) |
The return value of the zip()
function can be –
- An empty iterator if we do not pass any parameter to the
zip()
function - An iterator of tuples with each tuple having only one element if a single iterable is passed to the
zip()
function - An iterator of tuples with each tuple having elements from all the iterables if multiple iterables are passed to the
zip()
function
1.2 Setting up Python
If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.
2. Python zip() function Example
I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of their choice.
2.1 zip() function
Let us understand this with the help of a code snippet.
zip() function
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # zip() method in python creates an iterator that will aggregate elements from two or more iterables. # Takes the iterable elements like input and returns the iterator. names = ( "John" , "Lucifer" , "Niklas" , "John" ) companies = ( "Microsoft" , "Apple" , "Oracle" , "Microsoft" ) # Using "list". zipped = list ( zip (names, companies)) print ( "Zipped result via List = {}" . format (zipped)) print ( "\n=====\n" ) # Using "set" will give back the unique value and the order of values won't be maintained. zipped1 = set ( zip (names, companies)) print ( "Zipped result via Set = {}" . format (zipped1)) print ( "\n=====\n" ) # Using "dict" format. zipped2 = dict ( zip (names, companies)) print ( "Zipped result via Dict = {}" . format (zipped2)) print ( "\n=====\n" ) # Using "for loop" to print the result # Using the "zipped" variable from line number 5. print ( "Printing the zipped result via a loop." ) for (key, value) in zipped: print (key, "-" , value) |
If everything goes well the following output will be shown in the IDE console.
Console Output
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | Zipped result via List = [('John', 'Microsoft'), ('Lucifer', 'Apple'), ('Niklas', 'Oracle'), ('John', 'Microsoft')] ===== Zipped result via Set = {('Lucifer', 'Apple'), ('Niklas', 'Oracle'), ('John', 'Microsoft')} ===== Zipped result via Dict = {'John': 'Microsoft', 'Lucifer': 'Apple', 'Niklas': 'Oracle'} ===== Printing zipped results via a loop. John - Microsoft Lucifer - Apple Niklas - Oracle John - Microsoft |
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
3. Summary
In this tutorial, we learned:
- An introduction to
zip()
function in the python programming language - Sample program to under the
zip()
function
You can download the source code of this tutorial from the Downloads section.
4. Download the Project
This was an example of zip()
function in the python programming language.
You can download the full source code of this example here: Python zip() function example