Python - Dictionary with Index as Value Last Updated : 08 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are having a list we need to find index of each element and store it in form of dictionary. For example, a = ['a', 'b', 'c', 'd'] we need to find index of each elements in list so that output should be {'a': 0, 'b': 1, 'c': 2, 'd': 3}.Using Dictionary ComprehensionWe can use dictionary comprehension to create a dictionary where each element of a list is mapped to its index. This is achieved by enumerating the list and constructing key-value pairs with elements as keys and their indices as values. Python a = ['a', 'b', 'c', 'd'] # Create a dictionary with elements as keys and their indices as values ind = {val: idx for idx, val in enumerate(a)} print(ind) Output{'a': 0, 'b': 1, 'c': 2, 'd': 3} Explanation:enumerate(a) generates index-value pairs from list which are used in dictionary comprehension to create key-value pairs.Resulting dictionary maps each element of the list to its corresponding index providing an efficient way to track positions.Using a LoopWe can use a for loop with enumerate() to iterate over the list and store each element as a key with its index as value in a dictionary. This approach builds dictionary step by step without using comprehension. Python a = ['a', 'b', 'c', 'd'] ind = {} # Iterate over the list with index-value pairs for idx, val in enumerate(a): # Assign the element as the key and its index as the value ind[val] = idx print(ind) Output{'a': 0, 'b': 1, 'c': 2, 'd': 3} Explanation:For loop iterates through the list using enumerate() extracting both index and element.Each element is stored as a key in dictionary with its corresponding index as value.Using dict() with zip()We can use dict(zip(lst, range(len(lst)))) to create a dictionary where each element of list is mapped to its index. zip() function pairs elements with their indices and dict() converts these pairs into a dictionary. Python a = ['a', 'b', 'c', 'd'] # Use zip() to pair elements with their indices and convert to a dictionary ind = dict(zip(a, range(len(a)))) print(ind) Output{'a': 0, 'b': 1, 'c': 2, 'd': 3} Explanation:zip(a, range(len(a))) pairs each element in the list with its corresponding index.dict() converts these key-value pairs into a dictionary mapping each element to its index efficiently. Comment More infoAdvertise with us Next Article Python Create Dictionary with Integer M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python dictionary-programs Practice Tags : python Similar Reads Add Key to Dictionary Python Without Value In Python, dictionaries are a versatile and widely used data structure that allows you to store and organize data in key-value pairs. While adding a key to a dictionary is straightforward when assigning a value, there are times when you may want to add a key without specifying a value initially. In 3 min read Initialize Python Dictionary with Keys and Values In this article, we will explore various methods for initializing Python dictionaries with keys and values. Initializing a dictionary is a fundamental operation in Python, and understanding different approaches can enhance your coding efficiency. We will discuss common techniques used to initialize 3 min read Python | Initializing dictionary with list index-values While working with Python we might need to perform tasks in which we need to assign a dictionary with list values as dictionary values and index as dictionary keys. This type of problem is quite common in cases we need to perform data-type conversion. Let's discuss certain ways in which this task ca 4 min read Python Create Dictionary with Integer The task of creating a dictionary from a list of keys in Python, where each key is assigned a unique integer value, involves transforming the list into a dictionary. Each element in the list becomes a key and the corresponding value is typically its index or a different integer. For example, if we h 3 min read 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 per 5 min read Get Index of Values in Python Dictionary Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe 3 min read Like