Get Index of Values in Python Dictionary Last Updated : 06 Feb, 2025 Comments Improve Suggest changes 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 Index of Values in Python Dictionary P pragya22r4 Follow Improve Article Tags : Python Python Programs python-dict Python dictionary-programs python +1 More Practice Tags : pythonpythonpython-dict Similar Reads Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or 9 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Logistic Regression in Machine Learning Logistic Regression is a supervised machine learning algorithm used for classification problems. Unlike linear regression which predicts continuous values it predicts the probability that an input belongs to a specific class. It is used for binary classification where the output can be one of two po 11 min read Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Like