Get Index of Values in Python Dictionary Last Updated : 06 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 when those values are stored in a list. For example, consider the following dictionary: a = {1: [1, 2], 2: [3, 4, 6]} here for each list stored as a value we want to retrieve the indices of its elements then the expected output for the example above is: [[0, 1], [0, 1, 2]]Using Enumerate and List ComprehensionThis is the simplest and most efficient way in which the built-in enumerate() function lets us loop over a list while keeping track of each element’s index. We can combine enumerate() with list comprehension to generate the index lists for all dictionary values. Python a = {1: [1, 2],2: [3, 4, 6]} # Using list comprehension with enumerate to get indices of all values res = [[i for i, val in enumerate(x)] for x in a.values()] print(res) Output[[0, 1], [0, 1, 2]] Explanation:We loop over every list (value) in the dictionary using a.values() and for each list x, we use enumerate(x) to obtain a pair of index i and its corresponding value val.list comprehension collects the index i for every element in xUsing Dictionary ComprehensionIn this method we use dictionary comprehension along with enumerate() to create a new dictionary that maps each original key to its list of indices. Python a = {1: [1, 2],2: [3, 4, 6]} # Using dictionary comprehension with enumerate to get index mapping for each key res = {key: [i for i, val in enumerate(x)] for key, x in a.items()} print(res) Output{1: [0, 1], 2: [0, 1, 2]} Explanation:dictionary comprehension iterates over each key-value pair using a.items() and for each key the corresponding value (which is a list) is processed with enumerate() to extract its indices.This creates a new dictionary where each key is associated with a list of indices for its value.Using a Simple LoopIf you prefer using loops for clarity a regular loop can also achieve the same result, this method builds the index lists step by step. Python a = {1: [1, 2],2: [3, 4, 6]} res= [] # Loop over each list in the dictionary for x in a.values(): indices = [] for i in range(len(x)): indices.append(i) res.append(indices) print(res) Output[[0, 1], [0, 1, 2]] Explanation:The code iterates through each list (the values of the dictionary) and, for each list, it creates a new list of indices corresponding to the positions of the elements (using range(len(x))).Each list of indices is appended to the result list res and finally the complete list of indices for all dictionary values is printed. Comment More infoAdvertise with us Next Article Get Total Keys in Dictionary - Python P pragya22r4 Follow Improve Article Tags : Python Python Programs python-dict Python dictionary-programs python +1 More Practice Tags : pythonpythonpython-dict Similar Reads Get List of Values From Dictionary - Python We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3].Using dict.values()We can use dict.values() along with the list() function to get the list. Here, t 2 min read Get Dictionary Value by Key - Python We are given a dictionary and our task is to retrieve the value associated with a given key. However, if the key is not present in the dictionary we need to handle this gracefully to avoid errors. For example, consider the dictionary : d = {'name': 'Alice', 'age': 25, 'city': 'New York'} if we try t 3 min read Get Total Keys in Dictionary - Python We are given a dictionary and our task is to count the total number of keys in it. For example, consider the dictionary: data = {"a": 1, "b": 2, "c": 3, "d": 4} then the output will be 4 as the total number of keys in this dictionary is 4.Using len() with dictThe simplest way to count the total numb 2 min read Get Total Keys in Dictionary - Python We are given a dictionary and our task is to count the total number of keys in it. For example, consider the dictionary: data = {"a": 1, "b": 2, "c": 3, "d": 4} then the output will be 4 as the total number of keys in this dictionary is 4.Using len() with dictThe simplest way to count the total numb 2 min read Python - Print dictionary of list values In this article, we will explore various ways on How to Print Dictionary in Python of list values. A dictionary of list values means a dictionary contains values as a list of dictionaries in Python. Example: {'key1': [{'key1': value,......,'key n': value}........{'key1': value,......,'key n': value} 4 min read Key Index in Dictionary - Python We are given a dictionary and a specific key, our task is to find the index of this key when the dictionaryâs keys are considered in order. For example, in {'a': 10, 'b': 20, 'c': 30}, the index of 'b' is 1.Using dictionary comprehension and get()This method builds a dictionary using dictionary comp 2 min read Like