Python - Convert Index Dictionary to List
Last Updated :
25 Apr, 2023
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
# 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))
Output : The original dictionary is : {2: 'Gfg', 4: 'is', 6: 'Best'}
The converted list : [0, 0, 'Gfg', 0, 'is', 0, 'Best', 0, 0, 0]
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
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
# 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))
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 #3 : Using * operator and insert() method
- Create a list of 0's
- Later on access dictionary and insert value at key index in list
- Display list
Python3
# Python3 code to demonstrate working of
# Convert Index Dictionary to List
# 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
x = [0]*10
for i in list(test_dict.keys()):
x.insert(i, test_dict[i])
# printing result
print("The converted list : " + str(x))
OutputThe original dictionary is : {2: 'Gfg', 4: 'is', 6: 'Best'}
The converted list : [0, 0, 'Gfg', 0, 'is', 0, 'Best', 0, 0, 0, 0, 0, 0]
Time Complexity : O(N)
Auxiliary Space : O(N)
Similar Reads
Convert a Dictionary to a List in Python In Python, dictionaries and lists are important data structures. Dictionaries hold pairs of keys and values, while lists are groups of elements arranged in a specific order. Sometimes, you might want to change a dictionary into a list, and Python offers various ways to do this. How to Convert a Dict
3 min read
Python - Convert List to Index and Value dictionary Given a List, convert it to dictionary, with separate keys for index and values. Input : test_list = [3, 5, 7, 8, 2, 4, 9], idx, val = "1", "2" Output : {'1': [0, 1, 2, 3, 4, 5, 6], '2': [3, 5, 7, 8, 2, 4, 9]} Explanation : Index and values mapped at similar index in diff. keys., as "1" and "2". Inp
4 min read
Convert a List to Dictionary Python We are given a list we need to convert the list in dictionary. For example, we are given a list a=[10,20,30] we need to convert the list in dictionary so that the output should be a dictionary like {0: 10, 1: 20, 2: 30}. We can use methods like enumerate, zip to convert a list to dictionary in pytho
2 min read
Convert Nested Dictionary to List in Python In this article, weâll explore several methods to Convert Nested Dictionaries to a List in Python. List comprehension is the fastest and most concise way to convert a nested dictionary into a list.Pythona = { "a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}, } # Convert nested dictionary to a list of li
3 min read
Convert Dictionary to String List in Python The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st
3 min read
Convert List of Lists to Dictionary - Python We are given list of lists we need to convert it to python . For example we are given a list of lists a = [["a", 1], ["b", 2], ["c", 3]] we need to convert the list in dictionary so that the output becomes {'a': 1, 'b': 2, 'c': 3}. Using Dictionary ComprehensionUsing dictionary comprehension, we ite
3 min read
Python - Convert Matrix to Dictionary The task of converting a matrix to a dictionary in Python involves transforming a 2D list or matrix into a dictionary, where each key represents a row number and the corresponding value is the row itself. For example, given a matrix li = [[5, 6, 7], [8, 3, 2], [8, 2, 1]], the goal is to convert it i
4 min read
Convert List Of Dictionary into String - Python In Python, lists can contain multiple dictionaries, each holding key-value pairs. Sometimes, we need to convert a list of dictionaries into a single string. For example, given a list of dictionaries [{âaâ: 1, âbâ: 2}, {âcâ: 3, âdâ: 4}], we may want to convert it into a string that combines the conte
3 min read
Convert List of Dictionary to Tuple list Python Given a list of dictionaries, write a Python code to convert the list of dictionaries into a list of tuples.Examples: Input: [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] Output: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)] Below are various methods to co
5 min read
Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
3 min read