-2
list=['Mary','Bob','Linda']
dictionary={0:'Mary', 1: 'Anna', 2:'Bob', 3:'Alice', 4: 'Linda'}

if list in set(dictionary.values()):                                
        name = dictionary.get(None, list)

        values = itemgetter(!*dic)(dictionary)

How can I get keys from the dictionary as the code find out same values from the list and dictionary?

Second question: how can I get different values keys of the dictionary between a list and a dictionary? E,g: print out the keys are [1],[3]

Thank you

BobbyZhao
  • 13
  • 5
  • 2
    Don't use `list` as a variable name. It masks the type with the same name. – Arya McCarthy May 04 '17 at 22:15
  • 1
    Have you tried the docs? – rafaelc May 04 '17 at 22:16
  • `dictionary.keys()` for keys, `dictionary.values()` for keys's values and `dictionary.items()` will return a tuple with `(key,value)`. – Chiheb Nexus May 04 '17 at 22:16
  • `get` will return `None` by default if the key isn't there. – Peter Wood May 04 '17 at 22:16
  • 1
    I'm not sure I understand what you're trying to do. Can you tell us what the expected output of your (future) working code is? – Matthew Cole May 04 '17 at 22:16
  • 1
    @BobbyZhao The question was closed as a duplicate because of the title, but you are actually trying to do something different: I suggest editing to have a more descriptive title and a clear explanation of what you want the result to be. I would gladly vote to reopen after that. – brianpck May 04 '17 at 22:21
  • It seems he wants a list of keys of which the values match the items in the list of names. Give an example of your expected output and I suspect this can be solved quickly, e.g. `[0, 2, 4]`. – pylang May 04 '17 at 22:46
  • @pylang Thank you for the response. The code can print out keys are [0,2,4], how can I get this result? Thank you – BobbyZhao May 04 '17 at 22:54
  • Change the list of names to an non-builtin, e.g. `names = ["Mary", "Bob", "Linda"]`. Then use a comprehension `[k for k, v in d.items() if v in names]`. The output are the keys that match the lookup condition. – pylang May 04 '17 at 22:59
  • 1
    @pylang It works, Thank you very much :) – BobbyZhao May 04 '17 at 23:32
  • @pylang how can I get different values keys of the dictionary between a list and a dictionary? e,g [1,3] thank you – BobbyZhao May 05 '17 at 16:46
  • 1
    @pylang thank you! How is the code of it? I wrote something like this (k for k, v in dictionary.items() if v not in names), but some items are the same in the dictionary and the list. How to get unequal names? – BobbyZhao May 06 '17 at 20:56
  • It seems you are asking another question, and it is unclear. Consider looking into the mechanics of how list comprehensions work. https://stackoverflow.com/documentation/python/196/comprehensions/737/list-comprehensions#t=2017050623282111655 I'm sure it can help. – pylang May 06 '17 at 23:30
  • 1
    Thank you very much! :) @pylang – BobbyZhao May 11 '17 at 17:18

1 Answers1

0

I'm surprised you didn't try the obvious:

dictionary.keys()

Note: this was provided when the question explicitly asked "How to get all keys from the dictionary?".

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 3
    I'm surprised you didn't link to the duplicate :) http://stackoverflow.com/questions/16819222/how-to-return-dictionary-keys-as-a-list-in-python – brianpck May 04 '17 at 22:16
  • That's not what the OP is asking (although it is a little hard to tell). He wants the keys corresponding to each value (i.e. reverse lookup). You can tell because the `list` he uses as an example contains examples of the `dict`'s `.values()` – jez May 04 '17 at 22:16
  • Thank you for the response. I want to get the keys from the dictionary are just about matching with the values with list – BobbyZhao May 04 '17 at 22:18
  • This may help: http://stackoverflow.com/questions/483666/python-reverse-invert-a-mapping – jez May 04 '17 at 22:19
  • I'm not sure OP needs all keys. They just want to check the key is there before getting the value. There are probably better ways of writing the code, [AFNP](http://stackoverflow.com/questions/12265451/ask-forgiveness-not-permission-explain) or use `get` with a default. – Peter Wood May 04 '17 at 22:19
  • how can I get different values keys of the dictionary between a list and a dictionary? e,g [1,3] – BobbyZhao May 05 '17 at 16:48