Python - Index Directory of Elements
Last Updated :
08 Mar, 2023
Given a list of elements, the task is to write a python program to compute all the indices of all the elements.
Examples:
Input: test_list = [7, 6, 3, 7, 8, 3, 6, 7, 8]
Output: {8: [4, 8], 3: [2, 5], 6: [1, 6], 7: [0, 3, 7]}
Explanation: 8 occurs at 4th and 8th index, 3 occurs at 2nd and 5th index and so on.
Input: test_list = [7, 6, 3, 7, 8, 3, 6]
Output: {8: [4], 3: [2, 5], 6: [1, 6], 7: [0, 3]}
Explanation: 8 occurs at 4th index, 3 occurs at 2nd and 5th index and so on.
Method #1: Using dictionary comprehension + enumerate() + set()
In this, we get all the indices using enumerate() to append to index list for matching values. Dictionary comprehension is used for iteration of all elements in the list. The set() is used to get all the elements without repetition.
Python3
# Python3 code to demonstrate working of
# Index Directory of Elements
# Using dictionary comprehension + enumerate()
# initializing list
test_list = [7, 6, 3, 7, 8, 3, 6, 7, 8]
# printing original list
print("The original list is : " + str(test_list))
# getting each element index values
res = {key: [idx for idx, val in enumerate(test_list) if val == key]
for key in set(test_list)}
# printing result
print("Index Directory : " + str(res))
Output:
The original list is : [7, 6, 3, 7, 8, 3, 6, 7, 8]
Index Directory : {8: [4, 8], 3: [2, 5], 6: [1, 6], 7: [0, 3, 7]}
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using dictionary comprehension + groupby() + enumerate() + sorted() + itemgetter()
In this, we sort and group like elements, using groupby() and sorted(). The itemgetter(), is used to get values for sort by values from index and values extracted using enumerate(). Dictionary comprehension is used to get paired index of the grouped result.
Python3
# Python3 code to demonstrate working of
# Index Directory of Elements
# Using dictionary comprehension + groupby() +
# enumerate() + sorted() + itemgetter()
from itertools import groupby
from operator import itemgetter
# initializing list
test_list = [7, 6, 3, 7, 8, 3, 6, 7, 8]
# printing original list
print("The original list is : " + str(test_list))
# after grouping after sorting
# and rearranging and assigning values with index
res = {key: [idx for idx, _ in groups] for key, groups in groupby(
sorted(enumerate(test_list), key=itemgetter(1)), key=itemgetter(1))}
# printing result
print("Index Directory : " + str(res))
Output:
The original list is : [7, 6, 3, 7, 8, 3, 6, 7, 8]
Index Directory : {3: [2, 5], 6: [1, 6], 7: [0, 3, 7], 8: [4, 8]}
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The dictionary comprehension + groupby() + enumerate() + sorted() + itemgetter() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n), new dictionary of size O(n) is created where n is the number of elements in the dictionary
Similar Reads
Find Index of Element in Array - Python In Python, arrays are used to store multiple values in a single variable, similar to lists but they offer a more compact and efficient way to store data when we need to handle homogeneous data types . While lists are flexible, arrays are ideal when we want better memory efficiency or need to perform
2 min read
Find element in Array - Python Finding an item in an array in Python can be done using several different methods depending on the situation. Here are a few of the most common ways to find an item in a Python array.Using the in Operatorin operator is one of the most straightforward ways to check if an item exists in an array. It r
3 min read
Find index of element in array in python We often need to find the position or index of an element in an array (or list). We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we a
2 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
Python Loop through Folders and Files in Directory File iteration is a crucial process of working with files in Python. The process of accessing and processing each item in any collection is called File iteration in Python, which involves looping through a folder and perform operation on each file. In this article, we will see how we can iterate ove
4 min read
Accessing all elements at given list of indexes-Python Sometimes, you may have a list of data and a separate list of indexes and the goal is to extract only the elements at those specific positions. For example, given a list [10, 20, 30, 40, 50] and a list of indexes [1, 3, 4], you want to retrieve [20, 40, 50] the values at those index positions in the
2 min read
Handling " No Element Found in Index() " - Python The task of handling the case where no element is found in the index() in Python involves safely managing situations where the desired element does not exist in a list. For example, with the list a = [6, 4, 8, 9, 10] and the element 11, we want to ensure the program doesn't raise an error if the ele
3 min read
Python - Dictionary with Index as Value 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 comprehens
2 min read
How to Get Index of a Substring in Python? To get index of a substring within a Python string can be done using several methods such as str.find(), str.index(), and even regular expressions. Each approach has its own use case depending on the requirements. Letâs explore how to efficiently get the index of a substring.The simplest way to get
2 min read