Python – Convert Index Dictionary to List
Sometimes, while working with Python dictionaries, we can have a problem in which we have keys mapped with values, where keys represent list index where value has to be placed. This kind of problem can have application in all data domains such as web development. Let’s discuss certain ways in which this task can be performed.
Input : test_dict = { 1 : ‘Gfg’, 3 : ‘is’, 5 : ‘Best’ }
Output : [0, ‘Gfg’, 0, ‘is’, 0, ‘Best’, 0, 0, 0, 0, 0]Input : test_dict = { 2 : ‘Gfg’, 6 : ‘Best’ }
Output : [0, 0, ‘Gfg’, 0, 0, 0, ‘Best’, 0, 0, 0, 0]
Method #1 : Using list comprehension + keys()
The combination of above functions can be used to solve this problem. In this, we perform the task of assigning values to list checking for index, by extracting keys of dictionary for lookup.
# Python3 code to demonstrate working of # Convert Index Dictionary to List# Using list comprehension + keys() # initializing dictionarytest_dict = { 2 : 'Gfg', 4 : 'is', 6 : 'Best' } # printing original dictionaryprint("The original dictionary is : " + str(test_dict)) # Convert Index Dictionary to List# Using list comprehension + keys()res = [test_dict[key] if key in test_dict.keys() else 0 for key in range(10)] # printing result print("The converted list : " + str(res)) |
The original dictionary is : {2: 'Gfg', 4: 'is', 6: 'Best'}
The converted list : [0, 0, 'Gfg', 0, 'is', 0, 'Best', 0, 0, 0]
Method #2 : Using list comprehension + get()
The combination of above functions can be used to solve this problem. In this, we extract element using get(), so as to assign the default values harnessing default value initializing property of get().
# Python3 code to demonstrate working of # Convert Index Dictionary to List# Using list comprehension + get() # initializing dictionarytest_dict = { 2 : 'Gfg', 4 : 'is', 6 : 'Best' } # printing original dictionaryprint("The original dictionary is : " + str(test_dict)) # Convert Index Dictionary to List# Using list comprehension + get()res = [test_dict.get(ele, 0) for ele in range(10)] # printing result print("The converted list : " + str(res)) |
The original dictionary is : {2: 'Gfg', 4: 'is', 6: 'Best'}
The converted list : [0, 0, 'Gfg', 0, 'is', 0, 'Best', 0, 0, 0]
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


