``````

In :

import os

``````
``````

In :

# Let's talk about dict and zip
dict([('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)])

``````
``````

Out:

{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

``````
``````

In :

# zip can create that pairing for us by zippering up two sides (lists) together
zipped = zip(['a', 'b', 'c', 'd', 'e'], [0, 1, 2, 3, 4])
print(zipped)

``````
``````

<zip object at 0x7f76bc74c188>

``````
``````

In [ ]:

``````
``````

In :

# dict will accept any sequences of pairings (2-tuples)
# but what if we have two sequences of values that we want to "pair up"
# zip brings the two sides of a pair (the sides of a zipper) together in an alligned pairing
list(zip(['a', 'b', 'c', 'd', 'e'], [0, 1, 2, 3, 4]))

``````
``````

Out:

[('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)]

``````
``````

In :

# and a dict is can take a list of pairs, like before... so
dict(zip(['a', 'b', 'c', 'd', 'e'], [0, 1, 2, 3, 4]))

``````
``````

Out:

{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

``````
``````

In :

# and `range(5)` is a sequence (iterable)
dict(zip(['a', 'b', 'c', 'd', 'e'], range(5)))

``````
``````

Out:

{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

``````
``````

In :

# if we get the length wrong, the zipper will get "stuck" on the shortest sequence
dict(zip(['a', 'b', 'c', 'd', 'e'], range(4)))

``````
``````

Out:

{'a': 0, 'b': 1, 'c': 2, 'd': 3}

``````
``````

In :

# or because any sequence will do, and a str is a sequence of characters:
dict(zip('abcde', range(5)))

``````
``````

Out:

{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

``````
``````

In :

# What will happen if you try to coerce a list of 3-tuples into a dict?
dict([('a', 1, 2), ('b', 3, 4)])

``````
``````

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
1 # What will happen if you try to coerce a list of 3-tuples into a dict?
----> 2 dict([('a', 1, 2), ('b', 3, 4)])

ValueError: dictionary update sequence element #0 has length 3; 2 is required

``````
``````

In :

x = list(range(3))
y = list(range(4))
print(x)
print(y)
list(zip(x, y, range(5)))

``````
``````

[0, 1, 2]
[0, 1, 2, 3]

Out:

[(0, 0, 0), (1, 1, 1), (2, 2, 2)]

``````
``````

In :

x = [(0, 0, 0), (1, 1, 1), (2, 2, 2)]
list(zip(*x))

``````
``````

Out:

[(0, 1, 2), (0, 1, 2), (0, 1, 2)]

``````
``````

In :

list(zip(x, x, x))

``````
``````

Out:

[(0, 1, 2), (0, 1, 2), (0, 1, 2)]

``````