max
max

Reputation: 52243

printing tab-separated values of a list

Here's my current code:

print(list[0], list[1], list[2], list[3], list[4], sep = '\t')

I'd like to write it better. But

print('\t'.join(list))

won't work because list elements may numbers, other lists, etc., so join would complain.

Upvotes: 35

Views: 89958

Answers (3)

cred
cred

Reputation: 131

print('\t'.join([str(x) for x in list]))

Upvotes: 13

Glenn Maynard
Glenn Maynard

Reputation: 57474

print(*list, sep='\t')

Note that you shouldn't use the word list as a variable name, since it's the name of a builtin type.

Upvotes: 52

fabmilo
fabmilo

Reputation: 48330

print('\t'.join(map(str,list)))

Upvotes: 37

Related Questions