Python programming possesses various in-built data structures to make the programming faster and more efficient. The list is one of those data structures that is simple 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 code.
What are Lists?
The list in Python 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.
You can easily understand lists with their properties below:
- 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.
Here is an example of a simple list:
sample_list = ['FavTutor', 1, 2.3, ['Python', 'is', 'fun']] print(sample_list)
Output:
['FavTutor', 1, 2.3, ['Python', 'is', 'fun']]
How to Print a List in Python?
We usually require a list of values to be outputted in coding, so it is a must for a programmer to know this. Here are 5 different ways to print a list with Python code:
1) Using loops
The simplest and standard method to print a list in Python is by using loops such as a 'for' or 'while' loop. 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.
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 this alternative method:
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.
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:
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 the 'sep' attribute such as sep=”/n” or sep=”,”. Check out the below example for a clear understanding of the "*" operator.
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.
Example:
sample_list = ['Python', 'with', 'FavTutor', 1, 2, 3] print(' '.join(map(str, sample_list)))
Output:
Python with FavTutor 1 2 3
5) Using List Slicing
To begin, you must have the list you wish to print saved someplace in your code. The slicing operator, which is a colon (:) put between the starting and ending indexes of the slice you wish to print, is then used to print a specified area of the list.
Example:
my_list = ['apple', 'banana', 'orange', 'grape', 'peach'] print(my_list[1:4])
Output:
['banana', 'orange', 'grape']
Can we print a list in Python without using brackets?
Till now we have seen methods that print a list in Python but all these methods print a list with their brackets in place. Is there a method to print these lists without the brackets? Yes, we can. In order to print the lists without their brackets, we can use a method called List Comprehension.
With list comprehension, we can iterate over the list and print a number one by one in order to eliminate the brackets in the list.
Have a look at the code below to understand this concept better:
lis = [1, 2, 3, 4, 5] [print(i, end=' ') for i in lis]
Output:
1 2 3 4 5
How to print a numbered list in Python?
In Python, printing a numbered list is an easy process. Using the built-in enumerate() method, you may iterate through the list and output each item along with its matching number using a for loop. Here's an example of code to show you how we can print a numbered list in Python:
my_list = ['apple', 'banana', 'orange'] for i, item in enumerate(my_list): print(f"{i+1}. {item}")
Output:
1. apple 2. banana 3. orange
Here we use the enumerate method to create a tuple with the shape (index, value) for each item that we have in our list. We then iterate over these tuples to print out each element and its index.
Also, learn how to sort a python alphabetically, which is an important question asked in an interview.
Conclusion
We covered everything a programmer needs to know about printing a list in python. Lists are widely used in coding; it is one of the most common tasks. These methods and problems with definitely improve your skills. Happy Learning :)