Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Can only join an iterable" python error

I've already looked at this post about iterable python errors:

"Can only iterable" Python error

But that was about the error "cannot assign an iterable". My question is why is python telling me:

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

I don't know what I am doing wrong! I was following this thread:

Reverse word order of a string with no str.split() allowed

and specifically 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 adapter the code to make it have an input. But when I ran it, it said the error above. I am a complete novice 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 an input like apples and pears and then produce an output pears and apples.

Help would be appreciated!

like image 537
Derek Avatar asked Aug 21 '15 15:08

Derek


People also ask

Can only join an iterable append?

The “TypeError: can only join an iterable” error is caused when you try to join a value that is not iterable to a string. This can happen if you assign the value of a built-in list method such as sort() to a new variable and try to join the result of that operation to a string.

Can only assign an iterable meaning?

The Error message here “TypeError: can only assign an iterable” refers to a general class containing containers called iterables, the root of the the tree in the Section on More Python types.

What is iterable Python?

Iterable is an object which can be looped over or iterated over with the help of a for loop. Objects like lists, tuples, sets, dictionaries, strings, etc. are called iterables. In short and simpler terms, iterable is anything that you can loop over.

Why is string iterable in Python?

The list numbers and string names are iterables because we are able to loop over them (using a for-loop in this case).


2 Answers

splitlist1.reverse(), like many list methods, acts in-place, and therefore returns None. So tobereversedlist1 is therefore None, hence the error.

You should pass splitlist1 directly:

splitlist1.reverse()
reversedlist = ' '.join(splitlist1)
like image 79
Daniel Roseman Avatar answered Oct 20 '22 23:10

Daniel Roseman


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

splitlist1.reverse() returns None, None object not support iteration.

like image 20
Eds_k Avatar answered Oct 20 '22 23:10

Eds_k