While working with Python we might need to perform tasks in which we need to assign a dictionary with list values as dictionary values and index as dictionary keys. This type of problem is quite common in cases we need to perform data-type conversion. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using dictionary comprehension + len()
This task can be performed using the combination of above functions in which we perform the construction of dictionary using the dictionary comprehension and indexing the limited using the len function.
# Python3 code to demonstrate working of # Initializing dictionary with list index-values # Using dictionary comprehension + len() # initializing list test_list = ['Gfg', 'is', 'best'] # printing original list print("The original list is : " + str(test_list)) # Initializing dictionary with list index-values # Using dictionary comprehension + len() res = {x : test_list[x] for x in range(len(test_list))} # printing result print("The dictionary indexed as list is : " + str(res)) |
The original list is : ['Gfg', 'is', 'best']
The dictionary indexed as list is : {0: 'Gfg', 1: 'is', 2: 'best'}
Method #2 : Using dict() + enumerate()
The combination of these methods can also be used to perform this task. In this we use the quality of enumerate function to get the indices and dict() is used to convert the list to dictionary.
# Python3 code to demonstrate working of # Initializing dictionary with list index-values # Using dict() + enumerate() # initializing list test_list = ['Gfg', 'is', 'best'] # printing original list print("The original list is : " + str(test_list)) # Initializing dictionary with list index-values # Using dict() + enumerate() res = dict(enumerate(test_list)) # printing result print("The dictionary indexed as list is : " + str(res)) |
The original list is : ['Gfg', 'is', 'best']
The dictionary indexed as list is : {0: 'Gfg', 1: 'is', 2: 'best'}
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Initializing dictionary with list index values
- Python | Initializing dictionary with empty lists
- Python | Initializing multiple lists
- Python - Convert Dictionary Value list to Dictionary List
- Python program to update a dictionary with the values from a dictionary list
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python | Convert List of Dictionary to Tuple list
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Extract Dictionary values list to List
- Python - Check List elements from Dictionary List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

