Thursday, November 17, 2016

Creating dictionaries from two list/tuples

We have two lists/tuples of equal length and wan to create a dictionary from these two lists.

There are many ways but below I have stated three

1) Using dict constructor:

2) Using dict comprehensions:

3) Using itertools.izip for python versions < 3.0. if we choose to use previous 2 options for python 2.7 or other python versions less than 3.0, zip method will return a list which is not recommended when input lists are of big size as it will require more space and time. Instead we use itertools which return iterable object when used. for python 3 and higher zip has the same behavior as of izip

Let me know your suggestions and queries.

There was an error in this gadget