0

I have written code that should do as the title says but im getting "TypeError: can only join an iterable" on line 12, in reverse_words d.append(''.join(c))

here is my following code-

def reverse_words(text):
    #makes 'apple TEST' into ['apple', 'TEST']
    a = text.split(' ')
    d = []
    for i in a:
        #takes 'apple' and turns it into ['a','p','p','l','e']
        b = i.split()
        #takes ['a','p','p','l','e'] and turns it into ['e','l','p','p','a']
        c = b.reverse()
        #takes ['e','l','p','p','a'] and turns it into 'elppa'
        #appends 'elppa' onto d
        d.append(''.join(c))
        #whole thing repeats for 'TEST' as well
    #joins d together by a space and should print out 'elppa TSET'
    print(' '.join(d))

reverse_words('apple TEST')

I know it has to do something that I messed up with c but I cannot identify it.

trying to reverse words while maintaining order but i got a type error

yel fog
  • 13
  • 1
  • `c = b.reverse()` The `reverse()` list method does not return the reversed values; it reverses the list in-place, and returns None. For the behavior you want, use `c = reversed(b)`. – John Gordon Nov 28 '22 at 01:59
  • when I use this, it prints "apple TEST" instead of "elppa TSET" – yel fog Nov 28 '22 at 02:06
  • Probably because your comment `#takes 'apple' and turns it into ['a','p','p','l','e']` indicates a misunderstanding of what is actually happening. `split()` does NOT split a string into its individual letters. – John Gordon Nov 28 '22 at 02:10

3 Answers3

0

Perhaps you could consider utilizing str.join on a comprehension that uses list slicing to reverse a string:

def reverse_words(text: str) -> str:
  return ' '.join(word[::-1] for word in text.split(' '))
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
0

The reverse() method for lists reverses elements in-place and doesn't return an iterable, meaning it will return a none object if the method runs successfully. You can modify your code like this:

def reverse_words(text):
    #makes 'apple TEST' into ['apple', 'TEST']
    a = text.split(' ')
    d = []
    for i in a:
        #takes 'apple' and turns it into ['a','p','p','l','e']
        b = i.split()
        #takes ['a','p','p','l','e'] and turns it into ['e','l','p','p','a']
        b.reverse()
        #takes ['e','l','p','p','a'] and turns it into 'elppa'
        #appends 'elppa' onto d
        d.append(''.join(b))
        #whole thing repeats for 'TEST' as well
    #joins d together by a space and should print out 'elppa TSET'
    print(' '.join(d))

reverse_words('apple TEST')

You can also use the reversed() method if you want the reversed list in a new variable like so:

def reverse_words(text):
    #makes 'apple TEST' into ['apple', 'TEST']
    a = text.split(' ')
    d = []
    for i in a:
        #takes 'apple' and turns it into ['a','p','p','l','e']
        b = i.split()
        #takes ['a','p','p','l','e'] and turns it into ['e','l','p','p','a']
        c = reversed(b)
        #takes ['e','l','p','p','a'] and turns it into 'elppa'
        #appends 'elppa' onto d
        d.append(''.join(c))
        #whole thing repeats for 'TEST' as well
    #joins d together by a space and should print out 'elppa TSET'
    print(' '.join(d))

reverse_words('apple TEST')
-1
d.append('').join(d)

If this is what you intended try this, otherwise you're gonna need to call a new variable and call .join() on that

  • now im getting AttributeError: 'NoneType' object has no attribute 'join' – yel fog Nov 28 '22 at 01:57
  • I looked at the documentation (I would have been wrong anyways, the code I wrote should have made d = d.append and did d.join()... however I didn't see .join in numpy documentation, I did see numpy.char.join which is probably the one ngl – Atticus Reeves Nov 28 '22 at 02:34