DEV Community

Cover image for Python zip() Function.
Shaila B
Shaila B

Posted on

Python zip() Function.

Python zip() function :

zip() function. It is used when there are two or more iterables and they need to be aggregated into one iterable.

(or)

zip() method is to map the similar index of multiple containers so that they can be used just using as a single entity.

Syntax : zip(*iterators)

For references see the example below:

list1 = [1, 2, 3, 4]

list2 = [5, 6, 7, 8]

result = list(zip(list1, list2))

print(result)
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

[(1, 4), (2, 5), (3, 6)]

Enter fullscreen mode Exit fullscreen mode

When two lists have an equal number of elements, 3rd list with elements from both lists can be created using zip().

list1 = [1, 2, 3]

list2 = [4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

Here the list1 and list2 have an equal number of elements. The result list contains the list1 and list2 elements.

result = list(zip(list1, list2))

print(result)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

[(1, 4), (2, 5), (3, 6)]

Enter fullscreen mode Exit fullscreen mode

When two lists have an equal number of elements, we can create a dictionary with these elements, the elements of the first list will be considered as keys and the elements of the second list will be considered as values.

For example :

list1 = [1, 2, 3]

list2 = [4, 5, 6]

result = dict(zip(list1, list2))

print(result)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

{1: 4, 2: 5, 3: 6}

Enter fullscreen mode Exit fullscreen mode

Let's see some more examples below

Creating a list using columns and values or data.

columns = ['employee_id', 'employee_name', 'employee_salary']

values = [1, 'John', '50000']

employee_details = list(zip(columns, values))

print(employee_details)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

[('employee_id', 1), ('employee_name', 'John'), ('employee_salary', '50000')]

Enter fullscreen mode Exit fullscreen mode

Creating a dictionary using columns and values.

columns = ['employee_id', 'employee_name', 'employee_salary']

values = [1, 'Smith', 80000]

employee_details = dict(zip(columns, values))

print(employee_details)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

{'employee_id': 1, 'employee_name': 'Smith', 'employee_salary': 80000}

Enter fullscreen mode Exit fullscreen mode

We can apply zip() on tuples.

columns = ('employee_id', 'employee_name', 'employee_salary')

values = (1, 'Eshani', 50000)

employee_details = dict(zip(columns, values))

print(employee_details)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

{'employee_id': 1, 'employee_name': 'Eshani', 'employee_salary': 50000}

Enter fullscreen mode Exit fullscreen mode

zip() function for multiple data.

columns = ('employee_id', 'employee_name', 'employee_salary')

values = (
    (1, 'John', 50000),
    (2, 'Smith', 80000),
    (3, 'Ruhi', 60000),
    (4, 'Arnav', 50000),

)

Enter fullscreen mode Exit fullscreen mode

In the above, we have multiple employee data so here we are going to use list comprehension

We need to apply zip on each record to create a list of dictionaries. Example using list comprehensions. We are applying zip and then dictionary on each employee.

employee_details = [list(zip(columns, employee)) for employee in values]

print(employee_details)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

[[('employee_id', 1), ('employee_name', 'John'), ('employee_salary', 50000)], [('employee_id', 2), ('employee_name', 'Smith'), ('employee_salary', 80000)], [('employee_id', 3), ('employee_name', 'Ruhi'), ('employee_salary', 60000)], [('employee_id', 4), ('employee_name', 'Arnav'), ('employee_salary', 50000)]]

Enter fullscreen mode Exit fullscreen mode

We can use dictionary comprehension.

columns = ('employee_id', 'employee_name', 'employee_salary')

values = [
          (1, 'Teena', 80000),
          (2, 'Mouna', 60000),
          (3, 'Janvi', 40000),

         ]


employees_details = [dict(zip(columns, employee)) for employee in values]

print(employees_details)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

[{'employee_id': 1, 'employee_name': 'Teena', 'employee_salary': 80000}, {'employee_id': 2, 'employee_name': 'Mouna', 'employee_salary': 60000}, {'employee_id': 3, 'employee_name': 'Janvi', 'employee_salary': 40000}]

Enter fullscreen mode Exit fullscreen mode

Using the map as we are trying to apply a row-level transformation to convert each employee into the dictionary with columns as keys.

columns = ('employee_id', 'employee_name', 'employee_salary')

values = [
          (1, 'John', 50000),
          (2, 'Smith', 80000),
          (3, 'Sanvi', 60000)
         ]

employee_details = list(map(lambda employee: dict(zip(columns, employee)), values))

print(employee_details)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

[{'employee_id': 1, 'employee_name': 'John', 'employee_salary': 50000}, {'employee_id': 2, 'employee_name': 'Smith', 'employee_salary': 80000}, {'employee_id': 3, 'employee_name': 'Sanvi', 'employee_salary': 60000}]

Enter fullscreen mode Exit fullscreen mode

Printing data type of the first element employees_list

print(employee_details[0])

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

{'employee_id': 1, 'employee_name': 'John', 'employee_salary': 50000}

Enter fullscreen mode Exit fullscreen mode

Printing size of employees_list

print(len(employee_details))

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

3

Enter fullscreen mode Exit fullscreen mode

Top comments (0)