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:
- Using for loop
- Using the sep parameter in print()
- Convert a list to a string for display
- Using map() function
- Using list comprehension
- Using Indexing and slicing
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
# Python program to print list a = [ 1 , 2 , 3 , 4 , 5 ] # printing the list using loop for x in range ( len (a)): print a[x], |
1 2 3 4 5
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 ] # printing the list using * operator separated by comma print ( * a) # printing the list using * and sep operator print ( "printing lists separated by commas" ) print ( * a, sep = ", " ) # print in new line print ( "printing lists in new line" ) print ( * a, sep = "\n" ) |
the
1 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 the list using join function() print ( ' ' .join(a)) # print the list by converting a list of # integers to string a = [ 1 , 2 , 3 , 4 , 5 ] print str (a)[ 1 : - 1 ] |
Geeks 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
# Convert integers to string using map a = [ 1 , 2 , 3 , 4 , 5 ] print ( ' ' .join( map ( str , a))) print "in new line" print ( '\n' .join( map ( str , a))) |
Python
1 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
# Python program to print list print the list by using list comprehension a = [ 1 , 2 , 3 , 4 , 5 ] [ print (i, end = ' ' ) for i in a] print ( "\nIn new line" ) [ print (i) for i in a] |
1 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 ] #method 1 print (l[:]) #method 2 print (l[ 0 :]) #method 3 print (l[ 0 : len (l)]) |
say
[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)