Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Assign keys with Maximum Element Index
In Python, finding the index of the maximum element in a list is a common task. When working with dictionaries containing lists, we often need to replace each list with the index of its maximum element. This creates a mapping from keys to the positions of their largest values.
Problem Overview
Given a dictionary where each value is a list of numbers, we want to find the index of the maximum element in each list and assign that index as the new value for the key.
Example
Consider this input dictionary ?
input_dict = {'hello': [4, 2, 8],
'tutorialspoint': [5, 12, 10],
'python': [9, 3, 7],
'users': [3, 6, 1]}
print("Input dictionary:")
print(input_dict)
Input dictionary:
{'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]}
For each key, we find the maximum element and its index:
- 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
Required Functions
max() Function
The max() function returns the largest element in an iterable ?
numbers = [4, 2, 8]
print("Maximum element:", max(numbers))
Maximum element: 8
index() Function
The index() method returns the position of the first occurrence of a value ?
numbers = [4, 2, 8]
max_element = max(numbers)
print("Index of maximum element:", numbers.index(max_element))
Index of maximum element: 2
Method 1: Using For Loop
This approach iterates through each key-value pair and finds the maximum element index ?
# Input dictionary
input_dict = {'hello': [4, 2, 8],
'tutorialspoint': [5, 12, 10],
'python': [9, 3, 7],
'users': [3, 6, 1]}
print("Input dictionary:")
print(input_dict)
# Create empty dictionary for results
result_dict = {}
# Traverse through each key
for key in input_dict:
# Find maximum element and its index
max_element = max(input_dict[key])
max_index = input_dict[key].index(max_element)
result_dict[key] = max_index
print("\nResultant dictionary:")
print(result_dict)
Input dictionary:
{'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]}
Resultant dictionary:
{'hello': 2, 'tutorialspoint': 1, 'python': 0, 'users': 1}
Method 2: Using Dictionary Comprehension
Dictionary comprehension provides a more concise syntax for the same operation ?
# Input dictionary
input_dict = {'hello': [4, 2, 8],
'tutorialspoint': [5, 12, 10],
'python': [9, 3, 7],
'users': [3, 6, 1]}
print("Input dictionary:")
print(input_dict)
# Using dictionary comprehension
result_dict = {key: input_dict[key].index(max(input_dict[key]))
for key in input_dict}
print("\nResultant dictionary:")
print(result_dict)
Input dictionary:
{'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]}
Resultant dictionary:
{'hello': 2, 'tutorialspoint': 1, 'python': 0, 'users': 1}
Comparison
| Method | Code Lines | Readability | Performance |
|---|---|---|---|
| For Loop | More | Very Clear | Same |
| Dict Comprehension | One Line | Concise | Same |
Conclusion
Both methods effectively assign keys with maximum element indices. Use dictionary comprehension for concise code or a for loop when you need more readable, step-by-step logic. The max() and index() functions work together to find the position of the largest element in each list.
