How to print a list using a custom format in Python

Sometimes, you need to print a list elements in a specific format or custom format. This tutorial is about How to print a list using a custom format in python. This article covers the different methods available in Python to print a custom-formatted lists. We will discuss all of them one by one in detail.

  • Print a custom formatted list using while/for loop along with format method
  • using for loop along with enumerate function to print a list in custom format
  • Insert a specific character after every list element

This code is executed on the Google Colab. You can also use Anaconda platform. It an open source available free for source code.

Example 1:

You can also use for loop to customize the format of a list. In this example, we have used format method along with for loop.

#Example to demonstrate the working of enumerate function
x = [23,14,32,76,59]

for item in range(0,len(x)):
  print('item at index {} is {}'.format(item, x[item]))
item at index 0 is 23
item at index 1 is 14
item at index 2 is 32
item at index 3 is 76
item at index 4 is 59

Enumerate() function takes a collection such as list, tuple etc. and return an enumerate object which contains a key in the form of counter. This key assigns a counter to each element within that object thus making the access of items easier.

Let’s look at an example. Suppose X be the list. Pass this list X as an argument in the enumerate function. Let the output be y. This y is the ordered pair in which the first value is the default enumerate value which starts from 0, and the second value will be the corresponding enumerate number in a list.

#Example to demonstrate the working of enumerate function
x = [23,14,32,76,59]

y = enumerate(x)

print("output: ", list(y))
output:  [(0, 23), (1, 14), (2, 32), (3, 76), (4, 59)]

As mentioned previously, the enumerate function returns an object. Therefore, in order to obtain the list, you need to convert that object into a list for which we have passed the output y into list function.

Example 2:

Now, lets discuss how to print a list in a custom format using enumerate function.

One way is to use for loop along with enumerate function. First of all, define the desired format in which you want to print a list. Then apply for loop on the input list and print it into that format as shown in the code below.

#Example to demonstrate the working of enumerate function
x = [23,14,32,76,59]
y = enumerate(x)

#define the format
fmt = "%i: %i"

for item in enumerate(x):
  print(fmt%item)
0: 23
1: 14
2: 32
3: 76
4: 59

Example 3:

Sometimes you want to print a list by inserting a specific character between the items. For this, you can insert a separator using “sep” command. Pass the character that you want to insert in the “sep” command.

list_01 = [11,12,15,17,20]

print("Output: ",*list_01, sep='*')
Output: *11*12*15*17*20

You can also use this method on strings

list_01 = ["Elizabeth", "John", "David"]

print("Output: ",*list_01, sep='\n')
Output: 
Elizabeth
John
David

Here ‘/n’ is used to move to new line. The steric ‘*’ is added before list1 so that the character in separator function is inserted after every element.

Similarly, you can also insert multiple character such as comma ‘,’ along with tab ‘\t’.

print(*list_01, sep=',\t')

Elizabeth, John, David

Example 4

Another way is to format a list based on a custom separator tool and count function. We can use conditional statements such a loops or map function to iterate over the list elements. In this example, we will use a count function with a “sep” function and the map function to iterate and transform every element into a new variable by assigning a new updated value.

from itertools import count

string = {'oranges', 'apple', 'mango', 'cherry', 'pineapple'}

print(*(map('{}: {}'.format, count(), string)), sep="\n")
0: oranges
1: pineapple
2: mango
3: apple
4: cherry

Another method of printing a list in a custom format is by using an unpacking function in combination with a map function. Enumerate function maps item to the corresponding index in a set/tuple/list etc., using a separator “sep” function.

string = {'Pencil', 'Sharpener', 'Rubber', 'Ruler'}

print("Stationary Items:")

print(*(map('{0[0]}: {0[1]}'.format,enumerate(string))),sep="\n")
Stationary Items:
0: Rubber
1: Ruler
2: Pencil
3: Sharpener

Example 5

Another way is to use join function along with for loop and enumerate function.

string = {'Pencil', 'Sharpener', 'Rubber', 'Ruler'}

print("Stationary Items:")

print("\n".join('{}: {}'.format(*i) for i in enumerate(string)))
Stationary Items:
0: Rubber
1: Ruler
2: Pencil
3: Sharpener

Most of the methods of printing lists in a customized format have been discussed in this article. Different functions such as enumerate function, separator function, for loops, count, join, unpacked, and map function etc. are used to print the list in a required format. Let us know about your feedback in the comments.

Leave a Comment

Your email address will not be published.