user3748223
user3748223

Reputation: 85

python print a list of lists of integers and strings without brackets

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

Answers (4)

JuniorCompressor
JuniorCompressor

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

Andrew
Andrew

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

letsc
letsc

Reputation: 2567

s = [1,2,3]

print ';'.join(i for i in map(str,s))

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 113988

print primelist[1:-1]

might be enough

or more likely

print ";".join(map(str,primelist))

Upvotes: 1

Related Questions