What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Print a List in Python?

  • Nov 13, 2021
  • 5 Minutes Read
How to Print a List in Python?

 

Python programming possesses various in-built data structures to make the programming faster and efficient like any other programming language. All these data structures are sequential in nature and stores huge data collection in various formats, unlike others. The list is one of those python data structures that is simple yet powerful and stores a wide range of data under a single variable. In this article, we will study various methods to print a list in python with examples and output. But before that, let us have a brief introduction to the python list.

What are Lists in Python?

The list is one of the fundamental data structures used to store multiple items in a single variable. You can create a list by placing elements between the square brackets([]) separated by commas (,). A list can contain any number of elements of different data types such as integer, float, string, etc. Lists contain innumerable properties which make them different from other data structures. Let us see some of those properties below:

Properties of the list:

  • Ordered: The items inside the list have a defined order, and that order will not change.
  • Dynamic: The size of a dynamic list can be dynamically modified at runtime i.e. we can easily add or remove an item from the list during run time without specifying any size. 
  • Mutable: The elements of the list can be modified, individual elements can be replaced, and the order of elements can be changed even after the list has been created.
  • Indexing: List elements can be accessed using a numerical index in square brackets. List indexing is also zero-based.
  • Can contain duplicates: The list can contain duplicate elements.

 

The simplest list is the empty list with square brackets but no elements inside it ([]). It is worth noting that the empty list evaluates to False when used in a boolean context. Also, a list can also have another list as an element, widely known as a nested list.  

For example:

sample_list = ['FavTutor', 1, 2.3, ['Python', 'is', 'fun']]

print(sample_list)

 

Output:

['FavTutor', 1, 2.3, ['Python', 'is', 'fun']]

 

4 Methods to Print List in Python

1) Using loops

When you come across printing the list while programming, the first thought that comes to your mind is to print it using the loops. It is one of the standard methods used by most python programmers. Using for loop, you can traverse the list from the 0th index and print all the elements in the sequence. For this, you can also make use of the len() function and identify the length of the list to print the elements as shown in the below example:

For example:

sample_list = ['Python', 'with', 'FavTutor']

for i in range(0, len(sample_list)):

     print(sample_list[i])

 

Output:

Python 
with
FavTutor

 

Moreover, using for loop, you can also print the list by the below-given method:

 

For example:

sample_list = ['Python', 'with', 'FavTutor']

for item in sample_list:

     print(item)

 

Output:

Python 
with
FavTutor

 

2) Using join() method

When you are available with the list containing just the string elements, the best choice is to print the list using the join() function. join() is an in-build python function that takes your list as a parameter and prints it as shown in the below example.

For example:

sample_list = ['Python', 'with', 'FavTutor']

print( .join(sample_list))

 

Output:

Python with FavTutor

 

As mentioned earlier, the join() function works only when the list contains only string elements. But if there are integer elements in the list? In this situation, you can convert integers into strings and later use the join() function to print it as shown in the below example: 

For example:

sample_list = [1,2,3]

print(str(sample_list)[1:-1])

 

Output

1, 2, 3

 

3) Using the ‘*’ symbol: 

When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”. Check out the below example for a clear understanding of the "*" operator. 

For example:

sample_list = ['Python', 'with', 'FavTutor', 1, 2, 3]

print(*sample_list, sep=', ')

 

Output

Python, with, FavTutor, 1, 2, 3

 

4) Using map()

Another method is to make use of the map() method and convert each item of the list to a string. The map() function returns a map object(which is an iterator) after applying the given function to each item of a given iterable. Then use the join() function to join elements of the list using a separator as mentioned in the earlier method. The join() method creates and returns a new string by concatenating all the elements in the list, separated by a specified separator. Check out the below example for a better understanding of the map() method:

For example:

sample_list = ['Python', 'with', 'FavTutor', 1, 2, 3]

print(' '.join(map(str, sample_list)))

 

Output:

Python with FavTutor 1 2 3

 

Conclusion

So far we have seen that list is the sequential data structure in python which is highly preferred by every programmer in the tech world. As it is most widely used, printing the list is one of the common tasks while coding your program and getting the expected output. Therefore, we have presented multiple methods to print list in python and improve your coding efficiency. Still, if you face any difficulties, get in touch with our live Python tutors to solve your doubts.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Anisha Dhameja
I am Anisha Dhameja, a computer engineering student and technical content writer. I am passionate to learn more and more about the rising technologies of this new world. It makes me elated to share the vast expanse of my knowledge through the content I make, to enrich the knowledge and guide the thinkers of tomorrow.