Identifying Unique Palindromes and Their Frequencies

  • Share this:

Code introduction


This function takes a list as input, finds all unique palindromic words in the list, and returns a dictionary containing each palindromic word and its frequency.


Technology Stack : Set (set), List (list), Dictionary (dict), Reversing (string slicing), Palindrome checking (is_palindrome)

Code Type : Function

Code Difficulty : Intermediate


                
                    
def unique_palindromes_in_list(input_list):
    def is_palindrome(word):
        return word == word[::-1]

    def unique_elements(lst):
        return list(set(lst))

    def filter_palindromes(lst):
        return [word for word in lst if is_palindrome(word)]

    def count_elements(lst):
        return {word: lst.count(word) for word in unique_elements(lst)}

    unique_palindromes = filter_palindromes(input_list)
    palindrome_counts = count_elements(unique_palindromes)

    return palindrome_counts