[issue30305] python 2.7.13 join issue

Ezio Melotti report at bugs.python.org
Mon May 8 07:11:16 EDT 2017


Ezio Melotti added the comment:

I think you are misunderstanding how join works.
join is useful when you have a list of strings, and you want to combine them together, possibly specifying a separator.  The syntax is separator.join(list_of_strings), for example:
>>> '-'.join(['foo', 'bar', 'baz'])
'foo-bar-baz'

What you are doing is: new_item = new_item.join((item + ';'))
Here new_item is the separator, and (item + ';') is a string (a sequence of characters), so the separator is added between each character of the string:
>>> '-'.join('foobarbaz')
'f-o-o-b-a-r-b-a-z'

new_item will grow bigger and bigger, and since you keep adding it between each character of the item, Python will soon run out of memory:
>>> 'newitem'.join('foobarbaz')
'fnewitemonewitemonewitembnewitemanewitemrnewitembnewitemanewitemz'

You probably want to add the items to a new list, and after the for loop you just need to do '; '.join(new_list_of_items), or, if you want a ; at the end, you can add (item + ';') to the list and then use ' '.join(new_list_of_items).

I also suggest you to use the interactive interpreter to experiment with join.

----------
nosy: +ezio.melotti
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30305>
_______________________________________


More information about the Python-bugs-list mailing list