Python: Iterating through the columns in a list of list to find the palindromes

Viewed 288

Here I have a word list as:

[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]

And I have to display all the palindromes in this list which are in rows as well as columns. I have coded to find all the palindromes in the rows. But cannot implement a method to find the palindromes in the columns.

Here is my code so far:

result_1=""
if len(palindrome)==len_line_str:
    for row in range(len(palindrome)):
        for horizontal_line in range(len(palindrome[row])):
            if ''.join(palindrome[row])==''.join(reversed(palindrome[row])):
                result_1=''.join(palindrome[row])+" is a palindrome starting at ["+str(row)+"]["+str(row)+"] and is a row in the table"
    print(result_1)

Which will display the output:

rotor is a palindrome starting at [0][0] and is a row in the table

Where "rotor" is a palindrome.

I need a method to get the palindromes in the columns which are: "refer", "tenet", "radar"

Any help is much appreciated. Thanks in advance!

6 Answers
Related