-1

For example, the following code:

dict = {
"foo": "a",
"bar": "b",
"see": "c",
}

string = "foo in the city"

if any(x in string for x in dict):
    print(dict[x])

Is there any way to get x? (or get dict[x] through another method?)

Zombie
  • 21
  • 1
  • 2
  • 1
    Does this answer your question? [How to pick just one item from a generator?](https://stackoverflow.com/questions/4741243/how-to-pick-just-one-item-from-a-generator) – mkrieger1 May 18 '21 at 11:54
  • Actually, it's rather https://stackoverflow.com/questions/2361426/get-the-first-item-from-an-iterable-that-matches-a-condition – mkrieger1 May 18 '21 at 11:56
  • i.e., `next(x for x in dict if x in string)`. – mkrieger1 May 18 '21 at 11:56
  • Does this answer your question? [Get the first item from an iterable that matches a condition](https://stackoverflow.com/questions/2361426/get-the-first-item-from-an-iterable-that-matches-a-condition) – Pranav Hosangadi May 18 '21 at 14:43

7 Answers7

2

There are a few points that need to be rectified before the answer is provided :

  1. any() can be used only for iterable objects like a list, tuple or dictionary - hence the object that gets returned inside the condition you have defined wont be passed and would be unidentified for any()

  2. List Comprehensions are an amazing way to manipulate values of an iterable and return your desired value in the form of a list, which can further be modified or printed as per your desire

Moving on to the original solution:

dict = {
"foo": "a",
"bar": "b",
"see": "c",
}

string = "foo in the city"
list_final = [x for x in dict if x in string]

A good approach is to get the output in the form of a list element (in case of more than one element:

print(list_final)

If you want it as a single element value (as a string) - very few cases, such as the one here:

print(list_final[0])
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
0

Just put "x in string" inside brackets.

So you would get

dict = {
  "foo": "a",
  "bar": "b",
  "see": "c",
}

string = "foo in the city"

if any((x in string) for x in dict):
    print(dict[x])
svrem
  • 19
  • 1
  • 5
  • mind that this will only output the very last value that fulfills the conditions, not all of them. – jaaq May 18 '21 at 12:03
0
dict = {
"foo": "a",
"bar": "b",
"see": "c",
}

string = "foo in the city"

print([dict[x] for x in string.split() if x in dict])

# Output:
# ['a']
Алексей Р
  • 7,507
  • 2
  • 7
  • 18
  • don't use reserved keywords as ```dict``` – David Meu May 18 '21 at 11:59
  • @DavidMeu `dict` is not reserved word, see https://docs.python.org/3/reference/lexical_analysis.html#keywords – Алексей Р May 18 '21 at 12:05
  • reserved function – David Meu May 18 '21 at 12:21
  • Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. It'd also be useful to explain how this differentiates from the other six answers here, and under what circumstances your approach is preferred. – Jeremy Caney May 18 '21 at 20:50
0

It is not clear what you are trying to achieve. The solution you are searching for could be:

res = [x  for x in dict if x in string]

or

[print(x) for x in dict if x in string]

but the last one it is not the good way to use list comprehension.

p.s. you should not use dict as name for a dictionary

0

Assuming you want to get all values where their keys are inside your string, you can do the following:

mydict = {
"foo": "a",
"bar": "b",
"see": "c",
}

string = "foo in the city"

matches = [x for x in mydict if x in string]

for match in matches:
    print(mydict[match])

Try it online!

jaaq
  • 1,188
  • 11
  • 29
0

According to your need. I think this is what you want.

dict = {
"foo": "a",
"bar": "b",
"see": "c",
}

s = "foo in the city"

result = [dict[x] for x in s.split() if dict.get(x,0) != 0]
print(result)

If you want to just print

[print(dict[x]) for x in s.split() if dict.get(x,0) != 0]

Output:

  a
abdeali004
  • 463
  • 4
  • 9
-1

I think you want to check if any of the words in string (please do not name variables as their type) is in the dict? If so, maybe this helps:

# make it a list
words = string.split(' ')

x = 'foo'

if any(x in words for x in dict):
    print(dict[x])

# output:
# a
Rafaó
  • 591
  • 3
  • 14