-5

Example:

dict = {1: "X", 2: "Y", 3: "X"}

I want to print every key that has the value "X".

Desired Output:

1 3

Edit: Yeah, yeah, whatever. I worded it wrong when I searched for the question - it is probably a duplicate from what I've seen but I came up with a solution after all your... helpful comments.

item = (input("\nItem Name: ")).lower()
if item in recipes.values():
    print("\n--------------------[Recipes]----------------------")
    for key, value in recipes.items():
        if value == item:
            print(key)
    print("---------------------------------------------------")
Ryan
  • 59
  • 10
  • 4
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on his own. A good way to show this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/help/how-to-ask). – Rory Daulton Sep 22 '17 at 00:11
  • 2
    The terminology for dictionaries are pairs of `key:value` , you're wanting to ask how to print every key that has the value `"X"` – Davy M Sep 22 '17 at 00:11
  • @RoryDaulton I have no idea where to start, and Google can't tell me anything. – Ryan Sep 22 '17 at 00:13
  • @DavyM Heh... Fixed :) thx – Ryan Sep 22 '17 at 00:13
  • 2
    You want to start with `for`. [Iterating over dictionaries](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops). – MatsLindh Sep 22 '17 at 00:14
  • 2
    I just googled that... Maybe your keywords are off – OneCricketeer Sep 22 '17 at 00:14

2 Answers2

0
for key, value in dict.items():
if value == 'X':
    return key
Brandon Kheang
  • 597
  • 1
  • 6
  • 19
0

You are probably confusing dictionaries keys and values. Notice that in a dictionary

dict = {key1: value1, key2: value2...}
dict[key1] = value 1

you can use dict.keys() to iterate over the keys of the dictionary. you shuld try somthing like

for key in dict.keys():
    if dict[key] == 'X':
        print key
guy.S
  • 123
  • 1
  • 5