
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to create Python dictionary by enumerate function?
Python enumerate() function takes any iterable as argument and returns enumerate object using which the iterable can be traversed. It contains index and corresponding item in the iterable object like list, tuple or string.
Such enumerate object with index and value is then converted to a dictionary using dictionary comprehension.
>>> l1=['aa','bb','cc','dd'] >>> enum=enumerate(l1) >>> enum >>> d=dict((i,j) for i,j in enum) >>> d {0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd'}
- Related Articles
- How to create nested Python dictionary?
- How to create a dictionary in Python?
- How to create Python dictionary from the value of another dictionary?
- How to create Python dictionary with duplicate keys?
- How to create Python dictionary from JSON input?
- How to create an empty dictionary in Python?
- Enumerate() in Python
- What are the uses of the "enumerate()" function on Python?
- How to create a Python dictionary from text file?
- In Python how to create dictionary from two lists?
- How to create a dictionary of sets in Python?
- Python Program To Create A Dictionary With A Dictionary Literal
- How to sort a Python dictionary by datatype?
- How to sort a Python dictionary by value?
- How to pass a dictionary as argument in Python function?

Advertisements