"Can only join repeating" python error

I already reviewed this post about repeated python errors:

"Can only repeat" Python error

But this was due to the error "it is impossible to assign iterability." My question is: why does python tell me:

"list.py", line 6, in <module> reversedlist = ' '.join(toberlist1) TypeError: can only join an iterable 

I do not know what I am doing wrong! I followed this topic:

Reverse string word order without str.split () allowed

and in particular this answer:

 >>> s = 'This is a string to try' >>> r = s.split(' ') ['This', 'is', 'a', 'string', 'to', 'try'] >>> r.reverse() >>> r ['try', 'to', 'string', 'a', 'is', 'This'] >>> result = ' '.join(r) >>> result 'try to string a is This' 

and adapt the code so that it has an input. But when I launched it, he said the error above. I am a complete newbie, so could you please tell me what the error message means and how to fix it.

Code below:

 import re list1 = input ("please enter the list you want to print") print ("Your List: ", list1) splitlist1 = list1.split(' ') tobereversedlist1 = splitlist1.reverse() reversedlist = ' '.join(tobereversedlist1) yesno = input ("Press 1 for original list or 2 for reversed list") yesnoraw = int(yesno) if yesnoraw == 1: print (list1) else: print (reversedlist) 

The program should take input, such as apples and pears, and then produce output pears and apples.

Help will be appreciated!

+4
source share
2 answers

splitlist1.reverse() , like many list methods, acts in place and therefore returns None . Therefore, tobereversedlist1 is therefore None, therefore an error.

You must pass splitlist1 directly:

 splitlist1.reverse() reversedlist = ' '.join(splitlist1) 
+6
source

string join must satisfy the connection object to be iterated (list, tuple)

splitlist1.reverse () returns None, not a single object supports iteration.

0
source

All Articles