I want to check if the words in a list are palindromes, that is, if they read the same from right to left as from left to right.
This has occurred to me:
listT=['alba','aerea','alla','ana']
listF=['Roberto','Manuel','Jesus','Marea']
list1=listT + listF
for i in list1:
for j in list1[i]:
if id(j)==id(-j-1):
print('Es un palíndromo')
else:
print('No es un palíndromo')
But it gives error in the second for
:
TypeError: list indices must be integers or slices, not str
I had also thought of another way, using two indexes, but in Python you can't do it, for example:
lista1=['aerea','ana']
So that you can determine a letter from a list, for example the 'a' of "aerea":
lista1[0[0]]
And this way you could do:
listT=['alba','aerea','alla','ana']
listF=['Roberto','Manuel','Jesus','Marea']
list1=listT + listF
for i in list1:
for j in range(len(list1[i])):
if list1[i[j]]==list1[i[-j-1]]:
print('True')
else:
print('False')
This can't either. How would you determine a letter of a word from a list?
This function evaluates if a word is equal backwards and forwards or not:
The method used is to loop through the word as an array of characters, comparing the first against the last, the second against the penultimate, and so on. We make these comparisons by varying the index from zero to the middle of the word:
For each iteration, we compare a letter from the beginning against a letter from the end:
producing a logical list of values.
Finally we use the function
all
to determine if all comparisons returned true. That is the returned value.You can use this function to evaluate the list:
Demonstration
produces
The error is due to this line:
In the iteration you do above (
for i in list1:
),i
it is each element oflist1
, when you try to access an index like thislist1[i]
, you are using a string as an index of a list. Something like:list1["string"]
, and this is obviously not valid, which is why the error occurs.If you mean to access the index of a string, from the index of a list. The expression
lista[m[n]]
is not valid, it is as if m were a list of integers and the indexn
of happened tom
exist inlista
. In addition, it would have to be enclosed in parentheses to indicate the order of origin in the evaluation of the expression.The correct way would be
lista[m][n]
, because itlista[m]
returns the element at the positionm
oflista
and then the indexn
does the same withm
(which should be an iterable). However, this aspect is not relevant to the case, if there are nested loops.You can check if a word is the same from right to left as it is from left to right, by iterating through the indices of a string in opposite directions.
This example iterates through the indices of the string
palabra
to the right (i
) and to the left (j
), if the positioni
of the string is different from that ofj
it is returnedFalse
, if the cycle dies it will returnTrue
.So it occurs to me that you can write such a function and use it to verify that each word in a list is a palindrome or not.
Also instead of returning a value you can print messages on the screen. It is just an example, you will see how to adapt it to your case.
Edit:
You can use a
slice
as an index of a string to invert it, so if the comparison of the inverse string is equal to the original string, it's a palindrome.Where:
:
: The first colon without left or right operand, indicates that it is selected from the beginning to the end of the chain.:-1
: The negative step indicates that the string is to be read in reverse.You have a more complete explanation in this answer .
In python we can iterate through lists both ways or even element by element, just like you did on your first try. But it seems to me that you messed up trying to use two numeric loops instead of going through it first element by element and then by indices, to avoid those problems, it is better to name the variables in a way that we know what they contain just by reading them , they make the code much easier to follow and let's not miss so much :
I have left you in the comments the step-by-step explanations, if there is something you do not understand, do not hesitate to ask!
By the way, you're a bit confused here:
Accessing multiple lists is done like this:
list1[i][j]
, first one index and then the other