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
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
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
Implementing a Contacts directory in Python
Our task is to implement a smartphone directory that collects contact data from the user until the user prompts the program to. Contact data refers to the contact's name, phone number, date-of-birth, a category that contact belongs to (Friends, Family, Work, Other), e-mail address. The user may ente
12 min read
Python - Elements with same index
Given a List, get all elements that are at their index value. Input : test_list = [3, 1, 8, 5, 4, 10, 6, 9]Â Output : [1, 4, 6]Â Explanation : These elements are at same position as its number.Input : test_list = [3, 10, 8, 5, 14, 10, 16, 9]Â Output : []Â Explanation : No number at its index. Method
5 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
Python - Negative index of Element in List
We are given a list we need to find the negative index of that element. For example, we are having a list li = [10, 20, 30, 40, 50] and the given element is 30 we need to fin the negative index of it so that given output should be -3.Using index()index() method in Python searches for the first occur
3 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