
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Assign Keys with Maximum Element Index in Python
In Python, an element index refers to the position of an element within a sequence, such as a list or string. It represents the location of an element, starting from 0 for the first element and incrementing by 1 for each subsequent element. Assigning keys with the maximum element index means associating unique identifiers or labels with the highest possible index value in a sequence. This approach allows for easy access and retrieval of elements based on their positions using the assigned keys.
Example
Assume we have taken an input dictionary. We will now find the maximum element in the values of each key and assign its index to the key and print the resultant dictionary using the above methods.
Input
inputDict = {'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]}
Output
Resultant dictionary after assigning keys with maximum element index: {'hello': 2, 'tutorialspoint': 1, 'python': 0, 'users': 1}
In the above input dictionary, the maximum element of key hello is 8 and its index is 2. Hence that index of the maximum element is assigned to the key hello which is 2 and the same with the other elements also and then the resultant dictionary is printed.
hello: max element is 8 -> index is 2
'tutorialspoint': max element is 12 -> index is 1
'python': max element is 9 -> index is 0
'users': max element is 6 -> index is 1
index() function
The position at the first occurrence of the provided value is returned by the index() function.
Syntax
list.index(element)
max() function
The max() function returns the highest-valued item/greatest number in an iterable.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input dictionary.
Print the input dictionary.
Creating an empty dictionary using dict() function for storing a resultant dictionary.
Use the for loop to traverse through the keys of the input dictionary.
Get the index of the maximum element in the dictionary keys and store the same key with the maximum element index.
Print the resultant dictionary after assigning each key with a maximum element index.
Example 1: Using for loop, index() and max() functions
The following program returns a dictionary after assigning each key of an input dictionary with a maximum element index using for loop, index(), and max() functions
Example
# input dictionary inputDict = {'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]} # printing input dictionary print("Input dictionary:\n", inputDict) # empty dictionary for storing a resultant dictionary resultantDict = dict() # traversing through the keys of the input dictionary for k in inputDict: # Getting the maximum element index # Storing it as a value for the same key resultantDict[k] = inputDict[k].index(max(inputDict[k])) # printing resultant dictionary print("Resultant dictionary after assigning keys with maximum element index:\n", resultantDict)
Output
On executing, the above program will generate the following output
Input dictionary: {'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]} Resultant dictionary after assigning keys with maximum element index: {'hello': 2, 'tutorialspoint': 1, 'python': 0, 'users': 1}
Example 2: Using dictionary comprehension, index() and max() functions
In this example, we use dictionary comprehension which is the shortcut version of the above for loop.
The following program returns a dictionary after assigning each key of an input dictionary with a maximum element index using dictionary comprehension, index(), and max() functions
Example
# input dictionary inputDict = {'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]} # printing input dictionary print("Input dictionary:\n", inputDict) # Performing same using dictionary comprehension for concise syntax resultantDict = {k: inputDict[k].index(max(inputDict[k])) for k in inputDict} # printing resultant dictionary print("Resultant dictionary after assigning ks with maximum element index:\n", resultantDict)
Output
On executing, the above program will generate the following output
Input dictionary: {'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]} Resultant dictionary after assigning ks with maximum element index: {'hello': 2, 'tutorialspoint': 1, 'python': 0, 'users': 1}
Conclusion
In this article, we have learned 2 different methods to Assign keys with the Maximum element index. We learned how to find the dictionary's highest-valued element as well as its index. In the end, we learned how to use dictionary comprehension for concise syntax.