Python - Dictionary via zip function

Introduction

A standard way to initialize a dictionary is to combine its keys and values with zip and pass the result to the dict call.

The zip built-in function allows us to construct a dictionary from key and value lists.

Demo

print( list(zip(['a', 'b', 'c'], [1, 2, 3])) )        # Zip together keys and values 
# ww  w . j a  v  a 2  s . co m
D = dict(zip(['a', 'b', 'c'], [1, 2, 3])) # Make a dict from zip result 
print( D )

Result

Related Topic