How can I access the key of dictionary ,with matching values of it with another external list?

I want to access the key of dictionary by checking equality of its value with an element of another list.

def split(word):
    return [char for char in word]

word = '26kk15'

d={'3':['1','2'],'4':['5','6'],'s':['k','l']}
keys=list(d.keys())
l=len(d)
c=0
nl=split(word)
for k in range(0,len(nl)):
    for iu in d.values():
        for j in iu:
            if(j==nl[k]):
                print(keys[c])
        c+=1

I am getting list index out of range error. And if I remove the outer for loop it will give me output as 3 4 s,while I want n3n4nsnsn3n4.

Answer

c is going out of range because you don’t reset it to 0 each time you iterate over d.values().

But there’s no need for all those extra lists. Use .items() to iterate over both the keys and values of the dictionary, so you can print the key when you find a matching value.

You should use the in operator to test whether something is in the list, without another nested loop.

for letter in word:
    for key, chars in d.items():
        if letter in chars:
            print(key)
            break