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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert Index Dictionary to List
# Using list comprehension + keys()
  
# initializing dictionary
test_dict = { 2 : 'Gfg', 4 : 'is', 6 : 'Best'
  
# printing original dictionary
print("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)) 

chevron_right


Output :



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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert Index Dictionary to List
# Using list comprehension + get()
  
# initializing dictionary
test_dict = { 2 : 'Gfg', 4 : 'is', 6 : 'Best'
  
# printing original dictionary
print("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)) 

chevron_right


Output :

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.




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.