Python Forum

Full Version: How to create a long string by concatenating two lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
my question here
I want to create long_string by concatenating the items in the list previously created
and also I have to make sure to put a space betweeen the names in the list. I have written the following code. But it's giving me the following error. I dont know why.

Error:
File "<ipython-input-44-fee3cf9fac7b>", line 7
print ''.join(str(n)+s for (n,s) in zip(list1, list2))
^
SyntaxError: invalid syntax
list1 = [1,2,3,4,5]
list2 = ["one", "two", "three", "four", "five"]
print (''.join([str(a) +" "+ b for a,b in zip(list1,list2)]))
Error:
File "<ipython-input-44-fee3cf9fac7b>", line 7
print ''.join(str(n)+s for (n,s) in zip(list1, list2))
^
SyntaxError: invalid syntax
Works for me, although I would join on a space or other non-empty string.

From the error you've posted, I would expect the problem is actually on the line before, maybe a missing comma or quote.
you might want to add a newline:
print (''.join([str(a) +" "+ b + '\n' for a,b in zip(list1,list2)]))
" ".join(list1.extend(list2))
@wavic: I think they want to interleave them, not one then the other. And that doesn't work anyway.
(Jul-10-2017, 08:21 PM)wavic Wrote: [ -> ]
" ".join(list1.extend(list2))

Output:
>>> " ".join([].extend([])) Traceback (most recent call last):  File "<pyshell#1>", line 1, in <module>    " ".join([].extend([])) TypeError: can only join an iterable >>> print([].extend([])) None
An empty list is None, yes.

Anyway, it works for me. I've just tried it

l1 = list(range(10))
l2 = list(range(11, 20))

print(' '.join([str(i) for i in l1.extend(l2)]))
(Jul-10-2017, 08:47 PM)wavic Wrote: [ -> ]An empty list is None, yes.
I don't understand what you mean by this.

(Jul-10-2017, 08:47 PM)wavic Wrote: [ -> ]it works for me. I've just tried it
What output are you getting?

Output:
$ python Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 26 2016, 12:10:39) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> l1 = list(range(10)) >>> l2 = list(range(11, 20)) >>> print(' '.join([str(i) for i in l1.extend(l2)])) Traceback (most recent call last):  File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not iterable >>> ^D $ python3 Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> l1 = list(range(10)) >>> l2 = list(range(11, 20)) >>> print(' '.join([str(i) for i in l1.extend(l2)])) Traceback (most recent call last):  File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not iterable >>>
(Jul-10-2017, 08:47 PM)wavic Wrote: [ -> ]An empty list is None, yes.

Output:
>>> [] is None False
???
I was meaning it's equivalent when you get None

In [1]: l = []

In [2]: bool(l)
Out[2]: False

In [3]: bool(None)
Out[3]: False
Pages: 1 2