[toc]
Problem Statement: How to separate the items of a list in Python using tab as the delimiter?
Example: The following example demonstrates an example of the given problem statement.
# Given list lst = ['100', '200', '300', '400', '500'] # Some way to print the list values separated by tab # Expected Output --> 100 200 300 400 500
The problem is self-explanatory. Hence, without wasting too much time, let’s dive into the various ways of solving this problem.
Method 1: Using Escape Sequence \t
In Python, we can indicate multiple strings using the print statement. We have to just add a comma ‘,’ between them, and they will be printed with a space in the middle. Refer to the example below:
name = "Fin" print(" Hello", name) # Hello Fin
In Python strings, the backslash “\” is a unique character likewise called the escape character. It is utilized in addressing certain whitespace characters, e.g. “\t” is a tab and “\n” is a newline.
Example:
print("Hello \t Welcome to Finxter") # Hello Welcome to Finxter
On the other hand, prefixing a unique character with backslash “\” transforms it into a standard character. This is also known as escaping. For instance, “\'” is the single quote character. Here’s a related question: “How to print quotation marks in Python?” We can use escaping in this case as shown below:
print("I love \'Python\'") # I love 'Python'
You can directly use the escape sequence “\t
” tab character to print a list tab-separated in Python.
Solution:
# Given list lst = ['100', '200', '300', '400', '500'] print(lst[0]+"\t"+lst[1]+"\t"+lst[2]+"\t"+lst[3]+"\t"+lst[4])
Output:
100 200 300 400 500
Method 2: Assigning \t to Sep
There are two little-used arguments of the print function in Python. The argument sep
indicates the separator which is printed between the objects. The argument end
defines what comes at the end of each line.
Example:
a = 'hello' b = 'world' print(a, b, sep=' Python ', end='!') # hello Python world!
We can use the escape sequence “\t” and assign it to the argument “sep” to print the values of a list separated by tabs in Python.
Example:
# Function that will separate the list by tab def list_tab(lst): print(*lst, sep='\t') # Given list lst = ['Python', 'list', 'separated', 'by', 'tabs'] # Calling the function list_tab(lst)
Output:
Python list separated by tabs
What does *lst operator do?
We have used the * operator before lst in Python because *lst means iterable unpacking. The iterable gets unpacked at each iteration. This strategy is exceptionally helpful while printing the data with tab spaces without commas. Instead of developing various functions to eliminate the comma, you can use this unpacking operator to separate them.
Recommended Article: Python Print Function [And Its SECRET Separator & End Arguments]
Method 3: Using Map and Join
Before having a look at the solution, we need to understand a couple of built-in methods in Python.
map()
is an inbuilt method in Python that takes a function and an iterable as an input. It then executes the function by passing the iterable as an input to the function.str.join(iterable):
Concatenates the elements in aniterable
. The result is a string, whereas each element in the iterable are βglued togetherβ using the string on which it is called a delimiter.
Recommended Reads:
Thus, you can simply use the str
method and the list as arguments to the map method and then apply the join
method upon the output of the map
method by using the ‘\t’ escape sequence as with the delimiter.
Example:
lst = ['Python', 'list', 'separated', 'by', 'tabs'] print('\t'.join(map(str, lst)))
Output:
Python list separated by tabs
Method 4: Using join() with List Comprehension
In case you are not a big fan of the map()
method, then I have another workaround for you. You can use a list comprehension along with the join()
method to achieve your target.
Example:
lst = ['Python', 'list', 'separated', 'by', 'tabs'] print('\t'.join([str(x) for x in lst])) # Python list separated by tabs
List comprehension is a compact way of creating lists. The simple formula is [expression + context].
Expression: What to do with each list element?
Context: What elements to select? The context consists of an arbitrary number of for and if statements.
Example: [x for x in range(3)]
creates the list [0, 1, 2]
.
Recommended Tutorial: List Comprehension in Python β A Helpful Illustrated Guide
Conclusion
In this article, we discussed numerous ways of printing tab-separated values of a Python list. I hope it was helpful and it answered all your queries. For more solutions and discussions like this, please stay tuned and subscribe.
Nerd Humor
Recommended: Finxter Computer Science Academy
- Do you want to master the most popular Python IDE fast?
- This course will take you from beginner to expert in PyCharm in ~90 minutes.
- For any software developer, it is crucial to master the IDE well, to write, test and debug high-quality code with little effort.
Join the PyCharm Masterclass now, and master PyCharm by tomorrow!