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 
# from  w  w  w  . ja va2  s .  co  m
D = dict(zip(['a', 'b', 'c'], [1, 2, 3])) # Make a dict from zip result 
print( D )

Result

Related Topic