-3

I am looking to solve a problem to compare the string of the key and value of the same dictionary.

To return a dictionary of all key and values where the value contains the key name as a substring.

a = {"ant":"antler", "bi":"bicycle", "cat":"animal"}

the code needs to return the result:

b = {"ant":"antler", "bi":"bi cycle"}
Vitalizzare
  • 4,496
  • 7
  • 13
  • 32
sunhem
  • 3
  • 2
  • what exactly is the problem? – juanpa.arrivillaga Oct 13 '22 at 17:03
  • Welcome to Stack Overflow! Please take the [tour]. SO is a Q&A site, so what's your question? Presumably you need help with solving this, but what help do you need exactly? Check out [How to ask and answer homework questions](https://meta.stackoverflow.com/q/334822/4518341). (Maybe this isn't homework per se, but the same sort of ideas still apply.) See also [ask]. You can [edit] to clarify. – wjandrea Oct 13 '22 at 17:05

2 Answers2

1

You can iterate through the dictionary and unpack the key and the value at the same time this way:

b = {}
for key, value in a.items():
    if value in key:
        b[value] = key

This will generate your wanted solution. It does that by unpacking both the key and the value and checking if they match afterward. You can also shorten that code by using a dictionary comprehension:

b = {key:value for key, value in a.items() if key in value}

This short line does the exact same thing as the code before. It even uses the same functionalities with only one addition - a dictionary comprehension. That allows you to put all that code in one simple line and declare the dictionary on the go.

NicoFoth
  • 11
  • 2
0
answer = {k:v for k,v in a.items() if k in v}

Notes:

  • to iterate over key: value pair we use dict.items();
  • to check if a string is inside some other string we use in operator;
  • to filter items we use if-clause in the dictionary comprehension.

See also:

Vitalizzare
  • 4,496
  • 7
  • 13
  • 32
  • 1
    This seems to be a homework question, in which case, just *giving* the answer won't help OP learn very much. Instead, it'd be better to point them in the right direction, or at least describe how this works step-by-step. Check out [How to ask and answer homework questions](https://meta.stackoverflow.com/q/334822/4518341). (I didn't downvote though.) See also [answer]: "Brevity is acceptable, but fuller explanations are better." – wjandrea Oct 13 '22 at 17:09
  • Beside the point, but `dict.items()` is not a generator. Maybe you meant "iterable", but if you're iterating over it, of course it's an iterable, so you could just leave off the "as a" clause entirely. – wjandrea Oct 13 '22 at 17:39
  • @wjandrea Well, I didn't use the word _generator_ as a designation for a specific type of object. It could be _supplier_, or _provider_ of data, I think. Not sure though. I'm not fluent in English. Answering here is sort of a language practice. However, I agree that this word can be misleading and should be removed. – Vitalizzare Oct 13 '22 at 17:50
  • It's more about Python than English. [*Generator*](https://docs.python.org/3/glossary.html#term-generator) has a strict definition in Python terminology. (Although, the definitions in the glossary are slightly off. *Generator* on its own refers to a generator iterator, and what it calls a plain "generator", we would call a generator function I suppose.) – wjandrea Oct 13 '22 at 17:57