In this article, we will discuss different ways to print the list contents with a delimiter in Python.
Table Of Contents
By default, when we print a list in Python, it prints the contents of the list in a comma separated way, and enclosed in brackets. But it might be possible that sometimes we have different requirements. Like we want to print the list element with a different kind of delimiter like a column or a comma or with any other character but without brackets. For that we need different techniques and in this article we are going to discuss different techniques to achieve this. So let’s start.
Method 1: Using print() function
In this technique, we are just going to use the simple print() function.
In the print() function, instead of passing the list of items directly, we will apply the astrix operator on the list object to decouple all the internal elements and pass them as separate arguments to the print() function. Also we will pass a separate argument
sep in the print() function.
sep argument denotes as a delimiter / separator, that print() function will use while printing the provided elements on the console.
Let us see some examples,
- Count occurrences of an item in List in Python
- Check if All Elements in List are Unique in Python
- Python : How to remove element from a list by value or Index | remove() vs pop() vs del
- Get first character of each string in List in Python
In this example we will print the list elements separated by a colon,
listOfNumbers = [11, 12, 13, 14, 15, 16, 17, 18] # print list elements with colon as separator print(*listOfNumbers, sep=':')
It printed all the items in the list separated by colon and without any brackets.
Let’s see another example in which we will pass the comma as a separator. In this case print() function will print the list content separated by comma without any brackets.
listOfNumbers = [11, 12, 13, 14, 15, 16, 17, 18] # print list elements with comma as separator print(*listOfNumbers, sep=',')
Let’s see an another example, in which we have a list of strings and we will use the same logic. We will pass the list elements as separate arguments by applying the astrik operator on the list, and then we will also pass a separator as colon. It will print each string on the console separated by a colon.
listOfStrs = ['This', 'is', 'a', 'sample', 'text'] # print list elements with colon as separator print(*listOfStrs, sep=':')
Method 2: Using join() function
This technique is little different. Here first we will convert the list content to a string and then we will print that string on the console. Now as we want to print the list elements separated by given delimiter, so we will join them together by using the given delimiter.
For that we need to pass the list elements to the join() function of the string. But join() function expects all the elements of this given sequence to be of string type. Therefore first we will iterate over all the list elements and convert them to a string. Then we will pass them to the join() function. Now the join() method is a string class method. So we will use a delimiter string to call this join()) function. Inside the join()) function we will pass all the list elements. But before that we will make sure that all the list elements are converted into strings.
Let us see an example, where we have a list of integers. We will join all the elements in this list, separated by colons to create a single string value. Then we will print the string value on the console.
listOfNumbers = [11, 12, 13, 14, 15, 16, 17, 18] # join list elements with a comma as separator strValue = ':'.join(str(num) for num in listOfNumbers) print(strValue)
Let’s see another example, where we will use a comma as a separator, and join all the list elements to create a string. Then we will print it.
listOfNumbers = [11, 12, 13, 14, 15, 16, 17, 18] # join list elements with a comma as separator strValue = ','.join(str(num) for num in listOfNumbers) print(strValue)
If your list has only string elements, then you can directly pass them to the join()) function. It will join all the string elements by using the given separator, and return a single string value. Then we can print the string value. Let’s see this example,
listOfStrs = ['This', 'is', 'a', 'sample', 'text'] # join list elements with a comma as separator strValue = ','.join(listOfStrs) print(strValue)
Here we printed all the list items separated by a given delimiter.
We learned about different ways to print all items of a list with a delimiter in Python.