Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to find the palindromes in the dictionary, but my output isn't giving me all of them.

I have this so far

Palindrome[] := DictionaryLookup[y_ /; (y == StringReverse[y])]

And my the output just it gives me {a,I}. What's wrong with my input? I don't see it.


share|improve this question
Two underscores after your y should help – bobthechemist Oct 22 '13 at 0:37
Palindrome[] := DictionaryLookup[y__ /; (y == StringReverse[y])] – bobthechemist Oct 22 '13 at 0:38
Ah okay thanks. Why did I need two underscores? I read the reference on wolfram, but I'm not sure how it applies here. – jas Oct 22 '13 at 0:51
A single underscore is for a single character, and double underscore is for one or more. – bobthechemist Oct 22 '13 at 0:55
So by character, you mean the double underscore is applied to the y and that would give me more than a one letter word? – jas Oct 22 '13 at 0:58
up vote 9 down vote accepted

As mentioned in the comments, to search for multi-letter palindromes, two underscores are needed as described in the help for string patterns:

Palindrome[] := DictionaryLookup[y__ /; (y == StringReverse[y])]

Now, Mathematica is probably not good enough to come up with lyrical palindromes.

share|improve this answer

Mathematica 10.3 introduced the function PalindromeQ, which can be utilized here

palindrome[] := DictionaryLookup[y__ /; PalindromeQ[y]]

{"a", "aha", "aka", "bib", "bob", "boob", "bub", "CFC", "civic", "dad", "deed", "deified",
 "did", "dud", "DVD", "eke", "ere", "eve", "ewe", "eye", "gag", "gig", "huh", "I", 
 "kayak", "kook", "level", "ma'am", "madam", "mam", "MGM", "minim", "mom", "mum", "nan", 
 "non", "noon", "nun", "oho", "pap", "peep", "pep", "pip", "poop", "pop", "pup", "radar", 
 "redder", "refer", "repaper", "reviver", "rotor", "sagas", "sees", "seres", "sexes", 
 "shahs", "sis", "solos", "SOS", "stats", "stets", "tat", "tenet", "TNT", "toot", "tot", 
 "tut", "wow", "WWW"}

Unfortunately this is much slower (≈ 10 times slower) than using

DictionaryLookup[y__ /; y === StringReverse[y]]

but can be also used with the option IgnoreCase -> True:

DictionaryLookup[y__ /; PalindromeQ[y, IgnoreCase -> True]]
{"a", "Abba", "Ada", "aha", "aka", "Ana", "Anna", "Ava", "bib", "bob", "Bob", "boob", 
 "bub", "CFC", "civic", "dad", "deed", "deified", "did", "dud", "DVD", "Ede", "eke", 
 "ere", "eve", "Eve", "ewe", "eye", "gag", "gig", "Hannah", "huh", "I", "kayak", "kook", 
 "Laval", "level", "ma'am", "madam", "Malayalam", "mam", "MGM", "minim", "mom", "mum", 
 "nan", "Nan", "non", "noon", "nun", "oho", "Ono", "Otto", "pap", "peep", "pep", "pip", 
 "poop", "pop", "pup", "radar", "redder", "refer", "repaper", "reviver", "rotor", "sagas",
 "Salas", "sees", "seres", "sexes", "shahs", "sis", "solos", "SOS", "stats", "stets", 
 "tat", "tenet", "Tet", "Tevet", "TNT", "toot", "tot", "tut", "Tut", "wow", "WWW"}

However, for this using

DictionaryLookup[y__ /; ToLowerCase@y === StringReverse[ToLowerCase@y]]

is much faster.

share|improve this answer
interesting. why does the official implementation perform worse than the user-defined one? – sunt05 Oct 16 '15 at 5:18
@sunt05 PalindromeQ has additional overheads, especially for strings. – Karsten 7. Oct 16 '15 at 5:24
@sunt05 and it seems, many new functions, at least at first, perform worse, than what could be user-defined in previous versions. One example is ContainsAll and related functions. – LLlAMnYP Oct 16 '15 at 11:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.