What is a zip object?

Practicing Coding Interview Questions in Python

Kirill Smirnov

Data Science Consultant, Altran

Definition

zip - object that combines several iterable objects into one iterable object.

Set of Iterables

$\text{e}_\text{i}$ - an element from an Iterable

Practicing Coding Interview Questions in Python

Definition

zip - object that combines several iterable objects into one iterable object.

Combining several Iterables into a zip object

$\text{e}_\text{i}$ - an element from an Iterable

Practicing Coding Interview Questions in Python

Definition

zip - object that combines several iterable objects into one iterable object.

Iterating through the zip object

$\text{e}_\text{i}$ - an element from an Iterable

Practicing Coding Interview Questions in Python

Example

title = 'TMNT'
villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady']
turtles = {
    'Raphael': 'Sai', 'Michelangelo': 'Nunchaku',
    'Leonardo': 'Twin katana', 'Donatello': 'Bo'
}
result = zip(title, villains, turtles)
print(result)
<zip object at 0x7f37bab6e608>
Practicing Coding Interview Questions in Python

Traversing through a zip object

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
('N', 'Bebop', 'Leonardo')
('T', 'Rocksteady', 'Donatello')
Practicing Coding Interview Questions in Python

Returning a list of tuples

result = zip(title, villains, turtles)
tuples = list(result)
print(tuples)
[
    ('T', 'Shredder', 'Raphael'), ('M', 'Krang', 'Michelangelo'),
    ('N', 'Bebop', 'Leonardo'), ('T', 'Rocksteady', 'Donatello')
]
Practicing Coding Interview Questions in Python

zip object as Iterator

result = zip(title, villains, turtles)
next(result)
('T', 'Shredder', 'Raphael')
next(result)
('M', 'Krang', 'Michelangelo')
next(result)
('N', 'Bebop', 'Leonardo')
next(result)
('T', 'Rocksteady', 'Donatello')
next(result)
StopIteration
Practicing Coding Interview Questions in Python

zip object is expendable

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
('N', 'Bebop', 'Leonardo')
('T', 'Rocksteady', 'Donatello')
Practicing Coding Interview Questions in Python

zip object is expendable

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
...
for item in result:
    print(item)
# nothing
result = zip(title, villains, turtles)
tuples = list(result)
print(tuples)
[
    ('T', 'Shredder', 'Raphael'),
    ('M', 'Krang', 'Michelangelo'),
    ('N', 'Bebop', 'Leonardo'),
    ('T', 'Rocksteady', 'Donatello')
]
Practicing Coding Interview Questions in Python

'zip' object is expendable

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
...
for item in result:
    print(item)
# nothing
result = zip(title, villains, turtles)
tuples = list(result)
print(tuples)
[
    ('T', 'Shredder', 'Raphael'),
    ...
tuples = list(result)
print(tuples)
[]
Practicing Coding Interview Questions in Python

Unequal Iterable sizes

title = 'TMNT'
villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady']
turtles = {
    'Raphael': 'Sai', 'Michelangelo': 'Nunchaku',
    'Leonardo': 'Twin katana', 'Donatello': 'Bo'
}
Practicing Coding Interview Questions in Python

Unequal Iterable sizes

title = 'Teenage Mutant Ninja Turtles'
villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady']
turtles = {
    'Raphael': 'Sai', 'Michelangelo': 'Nunchaku',
    'Leonardo': 'Twin katana', 'Donatello': 'Bo'
}
result = zip(title, villains, turtles)
Practicing Coding Interview Questions in Python

Traversing through the 'zip' object

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('e', 'Krang', 'Michelangelo')
('e', 'Bebop', 'Leonardo')
('n', 'Rocksteady', 'Donatello')
Practicing Coding Interview Questions in Python

Reverse operation

turtle_masks = [
    ('Raphael', 'red'), ('Michelangelo', 'orange'),
    ('Leonardo', 'blue'), ('Donatello', 'purple')
]
result = zip(*turtle_masks)
print(result)
[
    ('Raphael', 'Michelangelo', 'Leonardo', 'Donatello'),
    ('red', 'orange', 'blue', 'purple')
]
Practicing Coding Interview Questions in Python

Unequal tuple sizes

turtle_masks = [
    ('Raphael', 'red'), ('Michelangelo', 'orange'),
    ('Leonardo', 'blue', 'cyan'), ('Donatello', 'purple', 'magenta')
]
result = zip(*turtle_masks)
print(result)
[
    ('Raphael', 'Michelangelo', 'Leonardo', 'Donatello'),
    ('red', 'orange', 'blue', 'purple')
]
Practicing Coding Interview Questions in Python

Relation to a dictionary

A zip object can be used to create a dictionary

keys = ['movie', 'year', 'director']
values = [
    ['Forest Gump', 'Goodfellas', 'Se7en'],
    [1994, 1990, 1995],
    ['R.Zemeckis', 'M.Scorsese', 'D.Fincher']
]
movies = dict(zip(keys, values))
print(movies)
{
    'director': [
        'R.Zemeckis',
        'M.Scorsese',
        'D.Fincher'
    ], 
    'movie': [
        'Forest Gump',
        'Goodfellas',
        'Se7en'
    ],
    'year': [1994, 1990, 1995]
}
Practicing Coding Interview Questions in Python

Creating a DataFrame

import pandas as pd

df_movies = pd.DataFrame(movies)
print(df_movies)
          director        movie  year
0  Robert Zemeckis  Forest Gump  1994
1  Martin Scorsese   Goodfellas  1990
2    David Fincher        Se7en  1995

list()

Practicing Coding Interview Questions in Python

Creating a DataFrame

import pandas as pd

df_movies = pd.DataFrame(movies)
print(df_movies)
          director        movie  year
0  Robert Zemeckis  Forest Gump  1994
1  Martin Scorsese   Goodfellas  1990
2    David Fincher        Se7en  1995

list() $\rightarrow$ zip()

Practicing Coding Interview Questions in Python

Creating a DataFrame

import pandas as pd

df_movies = pd.DataFrame(movies)
print(df_movies)
          director        movie  year
0  Robert Zemeckis  Forest Gump  1994
1  Martin Scorsese   Goodfellas  1990
2    David Fincher        Se7en  1995

list() $\rightarrow$ zip() $\rightarrow$ dict()

Practicing Coding Interview Questions in Python

Creating a DataFrame

import pandas as pd

df_movies = pd.DataFrame(movies)
print(df_movies)
          director        movie  year
0  Robert Zemeckis  Forest Gump  1994
1  Martin Scorsese   Goodfellas  1990
2    David Fincher        Se7en  1995

list() $\rightarrow$ zip() $\rightarrow$ dict() $\rightarrow$ DataFrame()

Practicing Coding Interview Questions in Python

Let's practice!

Practicing Coding Interview Questions in Python

Preparing Video For Download...