Print List in Custom Format in Python (Examples)
In this Python article, you’ll learn how to print a list in a custom format in Python.
Here is a quick overview of the tutorial:
Let’s dive into it!
Create Sample List
First of all, we will create a sample list that will be printed in a custom format in different ways. To do so, please run the code below.
my_list = [1, "dog", 2, "cat", 3, "horse", 4, 5] print(my_list) # [1, 'dog', 2, 'cat', 3, 'horse', 4, 5]
We have created a sample list named my_list
, which contains strings and integers. Let’s see how to print it in a custom way!
Example 1: Print List in Custom Format Using ‘*’ Operator
In Python 3, there’s an easy way to print list in a custom format by using the print() function. Check the code below.
print('My list:', *my_list, sep='\n- ') # My list: # - 1 # - dog # - 2 # - cat #Â - 3 # - horse # - 4 # - 5
The operator *
unpacks de elements of the list as separate arguments. Then, the sep
parameter is used to specify that each argument should be separated by the string '\n- '
, which adds the prefix -
before each item of the list and write them in a new line. Finally, the resulting string is printed using the print() function.
Example 2: Print List in Custom Format Using List Comprehension & join() Method
The list comprehension method and the print() function can also print a list in a custom way. Check the following output.
print("My list:") print('\n'.join(["- {}".format(item) for item in my_list])) # My list: # - 1 # - dog # - 2 # - cat #Â - 3 # - horse # - 4 # - 5
As shown in the above Python output, this example uses list comprehension to create a new list with each item in the original list formatted as a string with the prefix -
and then joins the strings using the join() method with the newline character '\n'
as a separator.
Example 3: Print List in Custom Format Using for Loop
Another common and simple way to print a list in a custom format is to use a for loop as follows.
print("My list:") for item in my_list: print("- {}".format(item)) # My list: # - 1 # - dog # - 2 # - cat #Â - 3 # - horse # - 4 # - 5
As you can see, the for loop iterates through each item in the list, and then the print() function displays them on the output console in the desired format via the string "- {}"
and format() method.
Example 4: Print List in Custom Format Using map() Function
In this example, we will use the map() function to print my_list
in a custom format. Let’s take a look!
print("My list:") print('\n'.join(map(lambda item: "- {}".format(item), my_list))) # My list: # - 1 # - dog # - 2 # - cat #Â - 3 # - horse # - 4 # - 5
This method uses the map() function to apply the lambda function to each item in the list, which formats each item as a string with the prefix -
. Then, the join() method is used to join the strings using the newline character as a separator. We finally obtained our custom-printed list 🙂
Video, Further Resources & Summary
Do you need more explanations on how to print a list in a custom format in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Moreover, there are other similar tutorials you may want to have a look at on our website:
- Create 2D List in Python
- Count Number of Integers in List in Python
- Convert Map Object to List in Python
- Count Occurrences of Each Element in List in Python
- Learn Python
This post has shown different ways to print a list in a custom format in Python. Don’t hesitate to let me know in the comments if you have further questions.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter