
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to create a dictionary with list comprehension in Python?
The zip() function which is an in-built function, provides a list of tuples containing elements at same indices from two lists. If two lists are keys and values respectively, this zip object can be used to constructed dictionary object using another built-in function dict()
>>> L1=['a','b','c','d'] >>> L2=[1,2,3,4] >>> d1=dict(zip(L1,L2)) >>> d1 {'a': 1, 'b': 2, 'c': 3, 'd': 4}
In Python 3.x a dictionary comprehension syntax is also available to construct dictionary from zip object
>>> L2=[1,2,3,4] >>> L1=['a','b','c','d'] >>> d={k:v for (k,v) in zip(L1,L2)} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}
- Related Articles
- Python Dictionary Comprehension
- How to catch a python exception in a list comprehension?
- Python - Create a dictionary using list with none values
- Python List Comprehension?
- Nested list comprehension in python
- How to create a dictionary in Python?
- Python – Create dictionary from the list
- Python List Comprehension and Slicing?
- How to create Python dictionary with duplicate keys?
- How to create Python dictionary from list of keys and values?
- How will you explain Python for-loop to list comprehension?
- Python Program – Create dictionary from the list
- How to convert Python Dictionary to a list?
- How to create a dictionary of sets in Python?
- How to create nested Python dictionary?

Advertisements