0

what I am trying to do: Create and print a list of words for which the reverse of the word is in the set of lower-case words. For example, "timer" and its reverse, "remit", are both in the lower-case word list.

what i have so far:

     s = set(lowers)
     [word for word in s if list(word).reverse() in s]

i just get an empty list.

1
  • 1
    -1. Learn to debug. This is at least the third question today where you could try things at the command line and see your error. The key is to break up your problem into smaller testable parts. In this case you could have gone to the command line and tried list('orange').reverse() and found that it returns None. Commented Nov 12, 2012 at 19:14

5 Answers 5

4

just use [::-1], using list() is unnecessary here and list(word).reverse() returns None as it changes the list in-place . You can also use "".join(reversed(word)), but I guess just word[::-1] is simple enough:

[word for word in s if word[::-1] in s]

In [193]: word="timer"

In [194]: print list(word).reverse()
None

In [195]: word[::-1]
Out[195]: 'remit'

In [196]: "".join(reversed(word))
Out[196]: 'remit'
Sign up to request clarification or add additional context in comments.

Comments

3

The list.reverse() method reverses a list in place. Its return value is None. If you want to reverse a string just use word[::-1].

Comments

2

Try this:

s = set(lowers)
[word for word in s if word.lower()[::-1] in s]

Comments

2

list.reverse() reverses the list in place and returns nothing. You need "".join(reversed(list)).

1 Comment

reversed(list) returns listreverseiterator, so he need more than just reversed(list).
0

You should be using [::-1] for reversing the string -

[word for word in s if word[::-1] in s]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.