Rearrange characters of a string into a palindrome, if possible I have written Python code which works absolutely fine but I am looking for other ways to do this:

import itertools
def permutation(word):
   for i in range(len(word)+1):
      perms=list(itertools.permutations(word))
      for i in perms:
         j=(''.join(i))
         j=j.casefold()
         k=reversed(j)
         if list(j)==list(k):
            print("P")
            print(j)
word='Aabbccdd'
permutation(word)
All that is technically required of a palindrome is that exactly one or zero of the character counts are odd and the rest are even. Then from this character count information you can reconstruct all of the palindromic words. Here is some python code to compute this method:

from collections import Counter
from itertools import permutations, repeat

def get_palindromes(word, ignore_case=True):
    if ignore_case:
        word = word.lower()
    char_counts = Counter(word)
    num_odd_counts = sum(int(c % 2 != 0) for c in char_counts.values())
    if num_odd_counts &gt; 1:
        return []
    odd_char = '' if num_odd_counts == 0 else [
        c for char, count in char_counts.items() if count % 2 == 1][0]
    even_chars = [rep_char for char, count in char_counts.items()
                  for rep_char in repeat(char, count / 2) if count % 2 == 0]
    return [perm_word + odd_char + perm_word[::-1]
            for perm_word in set(
                    ''.join(char_perm)
                    for char_perm in permutations(even_chars))]


If there are no palindromes this function will return an empty list, but you can obviously return any value you like in this case.