2

I have a dictionary which its keys has multiple values, I have a list which some of its items are values from the dictionary and some are not. what I'm trying to do is to read every item in that list and if it was a value in the dictionary, append the key pair of it to another list and if it wasn't append the item itself, but I keep get nothing

d = {'salam':('hi', 'ciao', 'bonjour'), 'mamnoon':('thanks', 'grazie', 'merci')}
l = ['hi', 'thanks', 'very', 'much']
l1 = []
for i in range(0, len(l)):
    search_val = l[i]
    for key, val in d.items():
        if val == search_val:
            l1.append(key)
        else:
            l1.append(l[i])
print(l1)

the output that I'm looking for is this but I keep receiving an empty list

['salam', 'mamnoon', 'very', 'much']
Palreza
  • 57
  • 6
  • 1
    You want `if search_val in val`, since `val` is a tuple of multiple strings. This will check if `search_val` is in this tuple for each pair in your dictionary. – dspencer Apr 08 '20 at 07:06

2 Answers2

2

You're looking for if search_val in val instead of if val == search_val, since val is a tuple of multiple strings and you don't want to test whether your search_val is equal to the full tuple, just whether the tuple contains your search_val.

Since your expected output includes those strings which could not be found in the values of your dictionary, you could write something like:

d = {'salam':('hi', 'ciao', 'bonjour'), 'mamnoon':('thanks', 'grazie', 'merci')}
l = ['hi', 'thanks', 'very', 'much']
l1 = []
for search_val in l:
    for key, val in d.items():
        if search_val in val:
            l1.append(key)
            break
    else:
        l1.append(search_val)
print(l1)

Output:

['salam', 'mamnoon', 'very', 'much']

Here, we stop searching the dictionary once we found the key with the search_val (since we assume that only one key will have this value in its tuple). The else clause of the for loop will be executed if the loop completes normally (i.e. does not exit because of hitting the break statement), which corresponds to the search_val not being found. In this case, we append the search_val to l1.

An alternative without using the for/else syntax could use a found flag:

d = {'salam':('hi', 'ciao', 'bonjour'), 'mamnoon':('thanks', 'grazie', 'merci')}
l = ['hi', 'thanks', 'very', 'much']
l1 = []
for search_val in l:
    found = False
    for key, val in d.items():
        if search_val in val:
            l1.append(key)
            found = True
    if not found:
        l1.append(search_val)
print(l1)
dspencer
  • 4,297
  • 4
  • 22
  • 43
  • this is the output that this code gives me: ['salam', 'thanks', 'mamnoon', 'very', 'very', 'much', 'much'] – Palreza Apr 08 '20 at 07:39
  • That is the output you get if you indent the `else` clause to the same level as the `if`. The `else` clause here should be indented to the same level as your inner `for` loop. See https://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops – dspencer Apr 08 '20 at 07:41
  • I have added an alternative, which doesn't use this syntax and uses a `found` boolean flag instead. This may be clearer as the `for/else` syntax is not so commonly used, but can work well in situations like this. – dspencer Apr 08 '20 at 07:44
1

your solution is almost true but you need to edit the condition in the inner for loop

val : is a tuple and the result of comparing a tuple and string will be false so it appends nothing and you will get an empty list.

instead of if val == search_val: try if search_val in val:

this will result in l1 = ['salam', 'mamnoon']