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.
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:
Moreover, using for loop, you can also print the list by the below-given method:
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.
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:
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.
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:
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 python experts to get help in your python assignments.