Python program to Convert a list into a dictionary with index as key
Last Updated : 29 Jan, 2025
We are given a list and our task is to convert it into a dictionary where each element’s index in the list becomes the key and the element itself becomes the corresponding value. For example, if we have a list like: ['apple', 'banana', 'cherry'] then the output will be {0: 'apple', 1: 'banana', 2: 'cherry'}.
Using enumerate()
enumerate()
function allows us to get both the index and value from the list simultaneously and makes this a simple and efficient way to create a dictionary where the index is the key.
Python li = ['aryan', 'harsh', 'kunal'] res = dict(enumerate(li)) print(res)
Output{0: 'apple', 1: 'banana', 2: 'cherry'}
Explanation: enumerate(li)
generates pairs like (0, 'apple'), (1, 'banana'), (2, 'cherry')
abnd dict()
converts these pairs into a dictionary
Using a Dictionary Comprehension
This method uses dictionary comprehension to iterate over the list and create key-value pairs where the index becomes the key and the element is the value.
Python li = ['aryan', 'harsh', 'kunal'] res = {i: li[i] for i in range(len(li))} print(res)
Output{0: 'aryan', 1: 'harsh', 2: 'kunal'}
Explanation: {i: li[i] for i in range(len(li))}
creates key-value pairs where the key is the index i
and the value is the corresponding element li[i]
.
Using zip()
with range()
zip()
function can be used to combine two iterables: one for the indices (range(len(li))
) and the other for the list values (li
). This creates pairs of indices and elements which can then be converted into a dictionary.
Python li = ['aryan', 'harsh', 'kunal'] res = dict(zip(range(len(li)), li)) print(res)
Output{0: 'aryan', 1: 'harsh', 2: 'kunal'}
Explanation of Code:
zip(range(len(li)), li)
pairs each index in range(len(li))
with the corresponding element in li
.dict()
converts these pairs into a dictionary.
Using a for
Loop
We can also use a simple for
loop to iterate through the list and add key-value pairs to an empty dictionary.
Python li = ['aryan', 'harsh', 'kunal'] res = {} for i in range(len(li)): res[i] = li[i] print(res)
Output{0: 'aryan', 1: 'harsh', 2: 'kunal'}
Explanation: for
loop iterates over the indices of the list and each index is used as a key in the dictionary with the corresponding list element as the value.
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 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
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 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
Python program to Convert Matrix to List of dictionaries Given a Matrix, convert it to a list of dictionaries by mapping similar index values. Input : test_list = [["Gfg", [1, 2, 3]], ["best", [9, 10, 11]]] Output : [{'Gfg': 1, 'best': 9}, {'Gfg': 2, 'best': 10}, {'Gfg': 3, 'best': 11}] Input : test_list = [["Gfg", [1, 2, 3]]] Output : [{'Gfg': 1}, {'Gfg'
4 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