Python Forum
How to create a long string by concatenating two lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a long string by concatenating two lists
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
you might want to add a newline:
print (''.join([str(a) +" "+ b + '\n' for a,b in zip(list1,list2)]))
Reply
#4
" ".join(list1.extend(list2))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
@wavic: I think they want to interleave them, not one then the other. And that doesn't work anyway.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(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
Reply
#7
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)]))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(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 >>>
Reply
#9
(Jul-10-2017, 08:47 PM)wavic Wrote: An empty list is None, yes.

Output:
>>> [] is None False
???
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Isolate a word from a long string nicocorico 2 1,527 Feb-25-2022, 01:12 PM
Last Post: nicocorico
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,792 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Create Dict from multiple Lists with duplicate Keys rhat398 10 4,062 Jun-26-2021, 11:12 AM
Last Post: Larz60+
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 2,820 Dec-15-2020, 05:21 PM
Last Post: snippsat
  concatenating 2 items at a time in a python list K11 3 2,315 Oct-21-2020, 09:34 AM
Last Post: buran
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,367 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Create array from string steve87bg 4 3,166 Jul-13-2020, 07:55 PM
Last Post: jefsummers
  join string lists redminote4dd 9 3,475 Jun-11-2020, 07:04 PM
Last Post: Yoriz
  concatenating lists in a comprehension Skaperen 3 2,119 Jan-01-2020, 08:10 PM
Last Post: ichabod801
  Split a long string into other strings with no delimiters/characters krewlaz 4 2,767 Nov-15-2019, 02:48 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020