Reputation: 85
I want to print this primelist:
sublist=["a","b","c"]
primelist=[sublist,["d",1,"e"],sublist]
I want this to be printed like that:
a;b;c
d;1;e
a;b;c
Here is my code:
for item in primelist:
print(";".join(item[0:]))
but I get this error:
>>> print(";".join(item[0:]))
TypeError: sequence item 1: expected str instance, int found
NOTE: if every item of the list is string then there is no problem
Upvotes: 1
Views: 439
Reputation: 20025
The problem is that 1
is not a string. You can solve it, using map(str, v)
for converting each item of a list v
to string:
>>> [";".join(map(str, v)) for v in primelist]
['a;b;c', 'd;1;e', 'a;b;c']
Or:
for v in primelist:
print ";".join(map(str, v))
Result:
a;b;c
d;1;e
a;b;c
Upvotes: 2
Reputation: 3871
The problem occurs in your line print(";".join(item[0:]))
. Since 1 is not a string in ["d', 1, "e"]
the .join() function fails. To fix:
for item in primelist:
print(";".join(map(str, item)))
Upvotes: 1
Reputation: 113988
print primelist[1:-1]
might be enough
or more likely
print ";".join(map(str,primelist))
Upvotes: 1