Python | Initializing dictionary with list index values

While working with dictionaries, we might come across a problem in which we require to attach each value in list with it’s index, to be used afterwards to solve question. This technique is usually very useful in competitive programming domain. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using dictionary comprehension and enumerate()
The combo of above methods can achieve this task. In this, enumerate()’s inbuilt capability to iterate value with it’s index is used to construct a key to corresponding value using dictionary comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Initializing dictionary with index value
# Using dictionary comprehension and enumerate()
  
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'CS']
  
# Printing original list 
print("The original list is : " + str(test_list))
  
# using dictionary comprehension and enumerate()
# Initializing dictionary with index value
res = {key: val for val, key in enumerate(test_list)}
  
# printing result 
print("Constructed dictionary with index value : " + str(res))

chevron_right


Output :



The original list is : ['gfg', 'is', 'best', 'for', 'CS']
Constructed dictionary with index value : {'gfg': 0, 'is': 1, 'best': 2, 'CS': 4, 'for': 3}

 

Method #2 : Using zip() + dict() + range() + len()
This task can also be performed by nesting the above functions. The task performed above by enumerate is handles by range and len functions and zip and dict perform the task of binding key with value and dictionary conversion respectively.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Initializing dictionary with index value
# Using zip() + dict() + range() + len()
  
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'CS']
  
# Printing original list 
print("The original list is : " + str(test_list))
  
# using zip() + dict() + range() + len()
# Initializing dictionary with index value
res = dict(zip(test_list, range(len(test_list))))
  
# printing result 
print("Constructed dictionary with index value : " + str(res))

chevron_right


Output :

The original list is : ['gfg', 'is', 'best', 'for', 'CS']
Constructed dictionary with index value : {'gfg': 0, 'is': 1, 'best': 2, 'CS': 4, 'for': 3}


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.