Printing lists in Python goes beyond a simple display of values; it empowers programmers to gain insights into their code’s behavior and verify data integrity. Join us on a journey of exploration as we uncover different strategies to print lists, complemented by practical use cases and best practices.
Input: lst = [2,5,6,8,9]
Output: 2 5 6 8 9
Explanation: In Output, we are printing the same list assigned to lst variable in the input.
Print lists in Python
Below are the methods that we will cover in this article:
Print list in Python using for loop
Traverse from 0 to len(list) and print all elements of the list one by one using a for loop, this is the standard practice of doing it.
Python
a = [ 1 , 2 , 3 , 4 , 5 ]
for x in range ( len (a)):
print a[x],
|
Time Complexity: O(n), where n is the length of a list.
Auxiliary Space: O(n), where n is the length of a list.
Print list using the sep parameter in print
The * symbol is used to print the list elements in a single line with space. To print all elements in new lines or separated by comma use sep=”\n” or sep=”, ” respectively.
Python
a = [ 1 , 2 , 3 , 4 , 5 ]
print ( * a)
print ( "printing lists separated by commas" )
print ( * a, sep = ", " )
print ( "printing lists in new line" )
print ( * a, sep = "\n" )
|
the
Output1 2 3 4 5
printing lists separated by commas
1, 2, 3, 4, 5
printing lists in new line
1
2
3
4
5
Time Complexity: O(n)
Auxiliary Space: O(1)
Convert a list to a string for display
If it is a list of strings we can simply join them using the join() function, but if the list contains integers then convert it into a string and then use the join() function to join them to a string and print the string.
Python
a = [ "Geeks" , "for" , "Geeks" ]
print ( ' ' .join(a))
a = [ 1 , 2 , 3 , 4 , 5 ]
print str (a)[ 1 : - 1 ]
|
OutputGeeks for Geeks
1, 2, 3, 4, 5
Time Complexity: O(n)
Auxiliary Space: O(1)
Print a list using the map() function
Use map() to convert each item in the list to a string if the list is not a string, and then join them with the help of the join function which joins the list.
Python
a = [ 1 , 2 , 3 , 4 , 5 ]
print ( ' ' .join( map ( str , a)))
print "in new line"
print ( '\n' .join( map ( str , a)))
|
Python
Output1 2 3 4 5
in new line
1
2
3
4
5
Time Complexity: O(n)
Auxiliary Space: O(1)
Print list in Python using list comprehension
Use list comprehension to go individually to each element in the list and print.
Python3
a = [ 1 , 2 , 3 , 4 , 5 ]
[ print (i, end = ' ' ) for i in a]
print ( "\nIn new line" )
[ print (i) for i in a]
|
Output1 2 3 4 5
In new line
1
2
3
4
5
Time Complexity: O(n)
Auxiliary Space: O(1)
Print a list using Indexing and slicing
We can print the list within a range or a complete list with the help of indexing we can select the range which we want to print and with the help of slicing we can extract that particular part from the list and then print it.
Python3
l = [ 1 , 2 , 3 , 4 , 5 , 6 ]
print (l[:])
print (l[ 0 :])
print (l[ 0 : len (l)])
|
say
Output[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
Note: If we don’t mention any index in slicing, it assumes 0 if we don’t say the starting range
(method 1 and method 2 are the examples) and if we don’t mention the ending range it assumes as the index of the last element (method 2 is the example). We can use the slice function also.
Time Complexity: O(n)
Auxiliary Space: O(n)