Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Assign K to Non Max-Min elements in Tuple
Next article icon

Python - Assign keys with Maximum element index

Last Updated : 27 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given Dictionary with value lists, the task is to write a Python program to assign each key with an index of the maximum value in the value list.

Examples:

Input : test_dict = {"gfg" : [5, 3, 6, 3], "is" : [1, 7, 5, 3], "best" : [9, 1, 3, 5]} Output : {'gfg': 2, 'is': 1, 'best': 0} Explanation : Max element in "gfg"'s value is 6 at 2nd index, hence assigned 2.  Input : test_dict = {"gfg" : [9, 3, 6, 3], "is" : [1, 7, 5, 3], "best" : [9, 1, 3, 5]} Output : {'gfg': 0, 'is': 1, 'best': 0 Explanation : Max element in "gfg"'s value is 9 at 0th index, hence assigned 0.

Method #1 : Using max() + loop + index()

In this, we get the index of the maximum element from the value list using max() and index(). Loop is used for the task of iteration of keys in the dictionary.

Python3
# Python3 code to demonstrate working of # Assign keys with Maximum element index # Using max() + index() + loop  # initializing dictionary test_dict = {"gfg": [5, 3, 6, 3], "is": [1, 7, 5, 3], "best": [9, 1, 3, 5]}  # printing original dictionary print("The original dictionary is : " + str(test_dict))  res = dict() for key in test_dict:      # using index() to get required value     res[key] = test_dict[key].index(max(test_dict[key]))  # printing result print("The maximum index assigned dictionary : " + str(res)) 

Output:

The original dictionary is : {'gfg': [5, 3, 6, 3], 'is': [1, 7, 5, 3], 'best': [9, 1, 3, 5]}

The maximum index assigned dictionary : {'gfg': 2, 'is': 1, 'best': 0}

Method #2 : Using dictionary comprehension + max() + index()

In this, we perform task of getting result using dictionary comprehension shorthand variation of above method.

Python3
# Python3 code to demonstrate working of # Assign keys with Maximum element index # Using dictionary comprehension + max() + index()  # initializing dictionary test_dict = {"gfg": [5, 3, 6, 3], "is": [1, 7, 5, 3], "best": [9, 1, 3, 5]}  # printing original dictionary print("The original dictionary is : " + str(test_dict))  # using dictionary comprehension as one liner alternative res = {key: test_dict[key].index(max(test_dict[key])) for key in test_dict}  # printing result print("The maximum index assigned dictionary : " + str(res)) 

Output:

The original dictionary is : {'gfg': [5, 3, 6, 3], 'is': [1, 7, 5, 3], 'best': [9, 1, 3, 5]}

The maximum index assigned dictionary : {'gfg': 2, 'is': 1, 'best': 0}

Method #3: Using a loop and the enumerate() function

Step-by-step algorithm:

  1. Initialize a dictionary test_dict with keys as strings and values as lists of integers.
  2. Print the original dictionary test_dict.
  3. Initialize an empty dictionary res.
  4. For each key-value pair in the dictionary test_dict, do the following:
    a. Get the maximum value of the list of integers using the max() function.
    b. Get the index of the maximum value in the list of integers using a list comprehension.
    c. Assign the key and the index value to the res dictionary.
  5. Print the resulting dictionary res.
Python3
# initializing dictionary test_dict = {"gfg": [5, 3, 6, 3], "is": [1, 7, 5, 3], "best": [9, 1, 3, 5]}  # printing original dictionary print("The original dictionary is : " + str(test_dict))  res = dict() for key, values in test_dict.items():     max_val = max(values)     max_idx = [i for i, v in enumerate(values) if v == max_val][0]     res[key] = max_idx  # printing result print("The maximum index assigned dictionary : " + str(res)) 

Output
The original dictionary is : {'gfg': [5, 3, 6, 3], 'is': [1, 7, 5, 3], 'best': [9, 1, 3, 5]} The maximum index assigned dictionary : {'gfg': 2, 'is': 1, 'best': 0}

Time Complexity: O(nm), where n is the number of keys in the dictionary test_dict and m is the length of the longest list of integers in the dictionary. 
Auxiliary Space: O(n), where n is the number of keys in the dictionary test_dict. 


Next Article
Python - Assign K to Non Max-Min elements in Tuple
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
Practice Tags :
  • python

Similar Reads

  • Python - K Maximum elements with Index in List
    GIven a List, extract K Maximum elements with their indices. Input : test_list = [5, 3, 1, 4, 7, 8, 2], K = 2 Output : [(4, 7), (5, 8)] Explanation : 8 is maximum on index 5, 7 on 4th. Input : test_list = [5, 3, 1, 4, 7, 10, 2], K = 1 Output : [(5, 10)] Explanation : 10 is maximum on index 5. Method
    4 min read
  • Python - Key with Maximum element at Kth index in Dictionary Value List
    Given a dictionary with values as lists, the task is to write a Python program to get the key with the maximum element at the Kth index by comparing the elements of each list. Input : test_dict = {'Gfg' : [4, 6, 8, 2], 'is' : [1, 4, 5, 9], 'best' :[2, 3, 4, 10], 'for' :[4, 5, 2, 1], 'geeks' :[2, 10,
    8 min read
  • Python | Maximum element in tuple list
    Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Let’s discuss certain ways in which this task can be performed. Method #1: U
    6 min read
  • Python - Elements Maximum till current index in List
    Given list with elements, extract element if it's the maximum element till current index. Input : test_list = [4, 6, 7, 8] Output : [4, 6, 7, 8] Explanation : All elements are maximum till their index. Input : test_list = [6, 7, 3, 6, 8, 7] Output : [7, 8] Explanation : 7 and 8 are maximum till thei
    7 min read
  • Python - Assign K to Non Max-Min elements in Tuple
    Sometimes, while working with Python data, we can have a problem in which we need to assign particular value as per certain condition. One of condition can be non-max, min element. This kind of task can occur in many application of day-day programming and competitive programming. Lets discuss certai
    7 min read
  • Python - Sort by Maximum digit in Element
    Given a List of Elements, sort by the maximum digit of the element present in the List. Input : test_list = [234, 92, 8, 721] Output : [234, 721, 8, 92] Explanation : 4 < 7 < 8 < 9, sorted by maximum digits. Input : test_list = [92, 8, 721] Output : [721, 8, 92] Explanation : 7 < 8 <
    6 min read
  • Python - Maximum and Minimum K elements in Tuple
    Sometimes, while dealing with tuples, we can have problem in which we need to extract only extreme K elements, i.e maximum and minimum K elements in Tuple. This problem can have applications across domains such as web development and Data Science. Let's discuss certain ways in which this problem can
    8 min read
  • Python - Minimum value key assignment
    Given two dictionaries, the task is to write a Python program to assign minimum values for matching keys from both dictionaries. Examples: Input : test_dict1 = {"gfg" : 1, "is" : 7, "best" : 8}, test_dict2 = {"gfg" : 2, "is" : 2, "best" : 10}Output : {"gfg" : 1, "is" : 2, "best" : 8}Explanation : Mi
    5 min read
  • Python | Index Maximum among Tuples
    Sometimes, while working with records, we might have a common problem of maximizing contents of one tuple with corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let’s discuss certain ways in which this task can be performed. Metho
    6 min read
  • Python - Maximum element in Cropped List
    Sometimes, while working with Python, we can have a problem in which we need to get maximum of list. But sometimes, we need to get this for between custom indices. This can be need of any domain be it normal programming or web development. Let's discuss certain ways in which this task can be perform
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences