Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to create Python dictionary by enumerate function?

Explain enumerate function. How to create Python dictionary by enumerate function?                       


1 Answer
Jayashree

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'}
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.