Looping through Dictionary and add items to second dictionary

I am trying to loop through a dictionary and add values that satisfy my true statemen to a second dictionary

At the moment, all I want to do is check and see if the value of each element is already a key in the entire dictionary and if so add it, otherwise go to next

mydict={“John” : “House”, “Eric” : “Turtle”, “Jimmy” : “John”, “Charles” : “Eric”}

myresult={“Jimmy” : “John”, “Charles” : Eric"} ← these two satisfy the true statement of the value being a key in the dictionary (i.e. value “John” is a key in “John”:“House”, etc.)

I’m pretty new and have no idea what the syntax should be… best i got is

mydict={‘John’ : ‘House’, ‘Eric’ : ‘Turtle’, ‘Jimmy’ : ‘John’, ‘Charles’ : ‘Eric’}
myresult={}
for x,y in mydict.items():
if y in mydict:
myresult.update({x,y})

which obviously doesn’t work otherwise i wouldn’t be posting here. This was copy and pasted from Jupytr so formatting may be off after submission

You didn’t say in what way it “doesn’t work”, but the problem is that {x,y} is a set (note the comma), not dict. It’s {x: y} for a dict.

Additionally, when pasting a block code, enclose it in triple backticks:

```
…your code…
```

to make it look like this:

...your code...

Thanks Matthew, I just figured that out, however i think i need to expand on my example since I got that error to pass but the returned dict is blank.

words.txt is just a file of dictionary words, easy palindrome project here, I just want to add the reverse of the word as the value, then ask if the reverse word is one of the key’s already, then it is a legit palindrome (not some muddled garbage) …but when i call actual_palindroms it is blank

words = {line.strip().lower() for line in open('words.txt', 'r')}

def reverse(word):
    return str(''.join(reversed(word)))

all_palindromes = {}
actual_palindromes={}

for word in words:
    all_palindromes[word]=str([reverse(word)])

for x,y in all_palindromes.items():
    if y in all_palindromes:
        actual_palindromes.update({x:y})

i’m “guessing” it may have something to do with the join, it looks like after i join the reverse it is a string and a set? e.g. ‘john’ : “[‘nhoj’]” but maybe i’m wrong, again i’m still pretty new

You wrote str([reverse(word)]), which will reverse the word, put it into a list (note the [...] there), and then convert that into a string.

Incidentally, .join returns a string, so you don’t need to use str(...) on the result. An even shorter method to reverse a string is to use slicing: word[ : : -1].

Can you describe this project? At first glance and without description of terms, conditions and algorithm it seems rather complicated way to determine whether word is palindrome or not.

Hi Charles,

You seem to be over-complicating your palindrome project.

A word is a palindrome if it is the same forwards and backwards. So all you need to do is start with a list of words, and check each word:

words = {line.strip().lower() for line in open('words.txt', 'r')}

palindromes = set()

for word in words:
    if word == word[::-1]:
        print(word, 'is a palindrome')
        palindromes.add(word)

Related to palindromes are words which form a different word when reversed. The most common word for those is, apparently (s)emordnilap. We can check for them as well:

words = {line.strip().lower() for line in open('words.txt', 'r')}

palindromes = set()
emordnilaps = set()

for word in words:
    backwards = word[::-1]
    if word == backwards:
        print(word, 'is a palindrome')
        palindromes.add(word)
    elif backwards in words:
        print(word, 'is an emordnilap')
        emordnilaps.add(word)

There’s no need for a dict in this.

2 Likes

I think you’re right :joy: I suppose I shouldn’t have dove into python after working for 12 hours but what you said makes sense, if it is same word is same forwards and backwards i’m all good; i got hung up on needing the reverse and went down a long unneeded rabbit hole