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.
You can directly use the escape sequence “ \t ” tab character to print a list tab-separated in Python.
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.
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.
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.
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.
print('\t'.join(map(str,list)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With