Skip to content
Fix Code Error

Get key by value in dictionary

March 13, 2021 by Code Error
Posted By: Anonymous

I made a function which will look up ages in a Dictionary and show the matching name:

dictionary = {'george' : 16, 'amber' : 19}
search_age = raw_input("Provide age")
for age in dictionary.values():
    if age == search_age:
        name = dictionary[age]
        print name

I know how to compare and find the age I just don’t know how to show the name of the person. Additionally, I am getting a KeyError because of line 5. I know it’s not correct but I can’t figure out how to make it search backwards.

Solution

There is none. dict is not intended to be used this way.

dictionary = {'george': 16, 'amber': 19}
search_age = input("Provide age")
for name, age in dictionary.items():  # for name, age in dictionary.iteritems():  (for Python 2.x)
    if age == search_age:
        print(name)
Answered By: Anonymous

Related Articles

  • How do I include certain conditions in SQL Count
  • How to make the overflow CSS property work with…
  • Form field border-radius is not working only on the…
  • How do I merge two dictionaries in a single…
  • What are the undocumented features and limitations…
  • What is The Rule of Three?
  • Python - How to assign/map non-sequential JSON…
  • Python is not calling fucntions properly
  • When dealing with Localizable.stringsdict, why…
  • How do I sort an observable collection?
  • What is a NullReferenceException, and how do I fix it?
  • XML Serialize generic list of serializable objects
  • How can I read inputs as numbers?
  • Asking the user for input until they give a valid response
  • HashMap with multiple values under the same key
  • Attempt to derive summarized list from extended list…
  • Creating Vue Search Bar | How to hide/show data…
  • How do you easily create empty matrices javascript?
  • Next.js - Warning: Prop `dangerouslySetInnerHTML`…
  • Vue - Deep watching an array of objects and…
  • Writing a dict to txt file and reading it back?
  • How to "perfectly" override a dict?
  • Setting Y limit of matplotlib range automatically
  • How can I get a List from some class properties with…
  • How to create dictionary and add key–value pairs…
  • Active tab issue on page load HTML
  • No matching function for to call operator ++ overloading
  • Extend Aurelia Validation Rules on a per class basis
  • Importing a function from a class in another file?
  • Are dictionaries ordered in Python 3.6+?
  • How do I establish a link/relationship between objects?
  • When use ResponseEntity and @RestController for…
  • Add non existings columns to mysql query result
  • What is attr_accessor in Ruby?
  • Why is lock(this) {...} bad?
  • Reuse calculated case column in where clause
  • Multi-dimensional arraylist or list in C#?
  • What is your most productive shortcut with Vim?
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Multiple scenarios @RequestMapping produces JSON/XML…
  • store struct of info in single linked list
  • Cannot update nested dictionary properly
  • What is an optional value in Swift?
  • What's the difference between eval, exec, and compile?
  • How to filter a RecyclerView with a SearchView
  • How to get the part of a file after the first line…
  • Retrieving a list of Dictionaries from Nested…
  • Vue.js 3: props type validation with custom type
  • Getting error while overriding class variables in dart
  • Getting the closest string match
  • Making a map of composite types typescript
  • Most effective way to parse JSON Objects
  • KeyError: 'user' when I use sessions with templates…
  • Search code inside a Github project
  • cannot call member function without object
  • Aurelia binding to a function with parameter and…
  • How do I print my Java object without getting…
  • What is a predicate in c#?
  • VueJS return value from promise in method section of…
  • AttributeError: 'dict' object has no attribute 'predictors'
  • Aurelia exported bundle causes a 404 from SystemJS…
  • How do you create nested dict in Python?
  • Weird binding with Ember.Select value
  • Loading image location from JSON file dynamically -…
  • How to use ES6 Fat Arrow to .filter() an array of objects
  • What's the best way to set variables only if they…
  • receiving json and deserializing as List of object…
  • How to create a search filter using Vue js from API Data?
  • Octave using 'for' statement to show two animations…
  • Javascript vs C# Reference Type
  • OpenCV Object detection with Feature Detection and…
  • Convert JSON File which contains multiple dictionary…
  • Is there any way to have TypeScript infer the type…
  • Make the size of a heatmap bigger with seaborn
  • Is it possible to make one SQL SELECT statement to…
  • More elegant way of declaring multiple variables at…
  • update file based on a key in C
  • Catch KeyError in Python
  • number of occurences of all values in an array as…
  • backbone js, Update view on model change
  • Safe method to get value of nested dictionary
  • Usage of __slots__?
  • Ember.js -- How do I target outlets in…
  • Image comparison - fast algorithm
  • TypeError: 'type' object is not subscriptable when…
  • Create a DataFrame from a XML File
  • Remove similar tuple from dictionary of tuples
  • How do you properly clear Aurelia binding of a…
  • How not to get a repeated attribute of an object?
  • best way to create object
  • In Aurelia computeds, when setting dependencies, how…
  • Multiple word search using trie in dart
  • MS Access SQL query - Count records until value is met
  • Reference - What does this regex mean?
  • Vue js v-link same route does not update content
  • How to initialize an array of custom objects
  • What's wrong with 'template int compare(char p1 [N],…
  • BTable Filter with a Function
  • How to loop back to start of question in case of…
  • How to override equals method in Java

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I check if string contains substring?

Next Post:

Read a file line by line assigning the value to a variable

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error