Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 910
max Avatar asked Oct 29 '10 04:10

max


People also ask

How do you print values separated by tab space in Python?

You can directly use the escape sequence “ \t ” tab character to print a list tab-separated in Python.

How do you print a list of elements separated by space?

Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.

How do I print a separator list in Python?

If you just want to know the best way to print a list in Python, here's the short answer: Pass a list as an input to the print() function in Python. Use the asterisk operator * in front of the list to “unpack” the list into the print function. Use the sep argument to define how to separate two list elements visually.

How do you separate a tab by value in Python?

split() method to split a string by tabs, e.g. my_list = my_str. split('\t') . The str. split method will split the string on each occurrence of a tab and will return a list containing the results.


2 Answers

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.

like image 184
Glenn Maynard Avatar answered Oct 13 '22 18:10

Glenn Maynard


print('\t'.join(map(str,list))) 
like image 26
fabrizioM Avatar answered Oct 13 '22 19:10

fabrizioM