trying to reverse words while maintaining order and print but having trouble figuring out the problem
Question:
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
Answers:
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
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(' '))
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')
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
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
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(' '))
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')