Print all items in a List with a delimiter in Python

In this article, we will discuss different ways to print the list contents with a delimiter in Python.

Table Of Contents

Introduction

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.

This 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,

Example 1:

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=':')

Output:

11:12:13:14:15:16:17:18

It printed all the items in the list separated by colon and without any brackets.

Example 2:

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=',')

Output:

11,12,13,14,15,16,17,18

Example 3:

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=':')

Output:

This:is:a:sample:text

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.

Example 1:

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)

Output:

11:12:13:14:15:16:17:18

Example 2

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)

Output:

11,12,13,14,15,16,17,18

Example 3

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)

Output:

This,is,a,sample,text

Here we printed all the list items separated by a given delimiter.

Summary

We learned about different ways to print all items of a list with a delimiter in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top